Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QListWidget or QListView with QItemDelegate?

Tags:

Let's say I need to display a list of items. Each item contains a QPushButton an image and some text. When a user clicks on the button something should happen (ie I need to get the signal). What is the right way to implement this in Qt?

After some reading, I understand that if I use a QListWidget and QListWidgetItem, this can be achieved. I can subclass each QListWidgetItem according to my needs and set them in the list widget.

However, I also read that a more appropriate approach (the Model View approach) is to use a QlistView coupled with a QItemDelegate. But if I'm using QItemDelegate, it seems that I can only paint the widgets. How can I get the push button event?

Sorry for this huge post. I'm kind of confused about the whole concept of when to use a QListWidget / QListView.

like image 617
Mahadevan Sreenivasan Avatar asked May 14 '11 16:05

Mahadevan Sreenivasan


1 Answers

QListWidget is essentially a customized version of QListView, designed for standard cases of list widgets, when all you're doing is just presenting image or text items in a list and the relationship with the underlying model is straightforward.

With QListWidget and its associated class QListWidgetItem you can e.g. insert and remove items very easily. But if you're inserting QPushButtons, then you can't use this so you might as well just use QListView and its (inherited) methods for setting the widget for a given index.

As for the signal, since you'll be creating the QPushButtons, just use

QObject::connect(my_button, SIGNAL(clicked()),...)  

to deal with that.

like image 100
Matt Phillips Avatar answered Jan 04 '23 01:01

Matt Phillips