QListWidget Add custom elements to Qt?

How to add 2 images and text to a QListWidget at runtime in Qt? I want to put one image at the beginning of the list and another at the end, and the text should be soon after my first image.

event with event

connect(list, SIGNAL(itemClicked()), this, SLOT(clicked(QListWidgetItem *))); void MyWidget::clicked(QListWidgetItem *item) { //code } 
+7
source share
1 answer

Take a look at the setItemWidget function. You can create a widget (name it MyListItemWidget ) that contains two icon labels and a text label, and two icons and text in your constructor. Then you can add it to your QListWidget . Code example:

 QIcon icon1, icon2; // Load them MyListItemWidget *myListItem = new MyListItemWidget(icon1, icon2, "Text between icons"); QListWidgetItem *item = new QListWidgetItem(); ui->listWidget->addItem(item); ui->listWidget->setItemWidget(item, myListItem ); 

You should also take a look at QListView and QItemDelegate , which is the best option for designing and displaying custom list items.

EDIT RELATIONSHIP TO YOUR CONNECTION

When connecting a signal to the slot, their signature must match. This means that a slot cannot have more parameters than a signal. From the documentation of signal slots

The mechanism of signals and slots is type-safe: the signature of the signal must match the signature of the receiving slot. (In fact, a slot may have a shorter signature than the signal it receives, as it may ignore additional arguments.)

This means that your signal must have a QListWidgetItem * argument in the connection.

 connect(list, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(clicked(QListWidgetItem *))) 
+10
source

All Articles