Adding custom widget to QListWidget in QT click issue in QT?

I am adding a custom widget to a QListWidget. My custom widget contains 2 shortcuts, 1 button
/ * My custom widget

UserDetails *myItem = new UserDetails(0," ALOA, Jeon"); UserDetails *myItem1 = new UserDetails(0," LOUIS, Stacy"); QListWidgetItem *item = new QListWidgetItem(); QListWidgetItem *item1 = new QListWidgetItem(); item->setSizeHint(QSize(0,45)); item1->setSizeHint(QSize(0,45)); ui->listWidget->addItem(item); ui->listWidget->addItem(item1); ui->listWidget->setItemWidget(item,myItem); ui->listWidget->setItemWidget(item1,myItem1); 

Using the code above, I add the mu Custom Widget item to the QListWidget. Now the problem is that I use One QPushButton in my CustomWidget now, when I click on the ListWidgetItem in the ListWidget, I want to change this button to a certain background image, and upon release it should return to its normal state. To do this, I use the stylesheet to do this, but when I click the button, it does not receive the click event of the List widget (itemclicked SLOT of List) when I double-click it.

How to get on one click?

+1
qt
source share
1 answer

It is not so simple, however it is doable. The problem is that when you click the button, the event event does not apply to the list widget. Therefore, you need to find a way to propagate the signal back to the list widget.

When the button is pressed, a pressed signal is issued. When it is released, the released signal is emitted. I will show you how to do this for one of them, you must do the same for the other.

The easiest way to do what you want is to add a connection between the pressed button signal and the slot that will change the background of your list. You have a QListWidget that has some UserDetails widgets, each of which has a QPushButton . So we have to go all the way from QPushButton to QListWidget .

In your UserDetails class UserDetails create a new signal, for example buttonPressed() , and connect the pressed() button to it. Thus, every time a button is pressed on your widget, the widget itself will notify the world that its button has been pressed.

 connect(ui->button, SIGNAL(pressed()), this, SIGNAL(buttonPressed()) 

Now we want to notify QListWidget that the button is pressed. To do this, we need to connect the buttonPressed signal from UserDetails using the slot, let us call the buttonPressedSlot() slot

 connect(myItem, SIGNAL(pressed()), this, SLOT(buttonPressedSlot()) 

Now the slot should determine what the sender is (since all UserDetails will be connected to the same slot, find the corresponding QListWidgetItem and call the slot that will update the background of this element.

 void LiwstWidgetClick::buttonPressedSlot() { QObject* signalSender = QObject::sender(); UserDetails* p = qobject_cast<UserDetails *>(signalSender); if (p == NULL) return; // Get Position in list widget for (unsigned i=0; i<ui->listWidget->count(); i++) { QListWidgetItem* item = ui->listWidget->item(i); if (ui->listWidget->itemWidget(item) == p) cellClicked(item); // THIS IS YOUR FUNCTION THAT CHANGES THE // BACKGROUND OF THE GIVEN ITEM } } 

You must do the same for the released() signal.

EDIT

If the button was public, you can avoid an additional signal, for example, if you had a function ( getButton() ) in UserDetails that returned ui->button , you could only have one connection

 connect(myItem->button(), SIGNAL(pressed()), this, SLOT(buttonPressedSlot())); 

EDIT 2

If you just want to change the background of your button when pressed, you can achieve this using stylesheets . You need entries in your stylesheet for the normal state of the button and for the state pressed . The list of available states looks here . Sample code follows

  QListView QPushButton { color: grey; border-image: url(/path/to/image1) 3 10 3 10; } QListView QPushButton:pressed { color: red; border-image: url(/path/to/image2) 3 10 3 10; } 

Note that I use the QListView Descendant Selector to get only these QPushButtons , which are children of the QListView

+3
source share

All Articles