View Full Version : How to build a list of custom widgets

28th July 2010, 10:13

I would like to create a list of custom widgets, each composed of 2 rows and 3 columns. The left and middle columns will contain text in each row and the right one will contain an icon spanning on both rows.

What is the best approach?

What I've done is to create a Widget containing a ListView and another Widget (which will certainly be a Table) that should be used as rows of the ListView.

I've read somewhere to use the setIndexWidget method but I don't exactly know how to use it.

Suppose I have a model containing the data to display, where shall the ListView be populated and how?

I would be really grateful if someone could provide a little example!

Thanks in advance!

28th July 2010, 10:14
By the way, I'm using Qt Jambi.

28th July 2010, 12:00
Will your text have editing options ?
also is 2 rows and 3 columns fixed ?

If yes I dont see need for QTableview.. you can do the following -
create 4 labels for text and 1 label for icon and place them in a grid layout. and apply the grid layout to your main custom widget
thats it.. !!

28th July 2010, 12:32
Okay, I see your point but I want the icon to span the 2 rows, can I do it using Grid Layout?

Moreover, do you know how I can insert my custom widget into my ListView?


28th July 2010, 12:42
but I want the icon to span the 2 rows, can I do it using Grid Layout?
Sure. Read documentation for QGridLayout

Why do you want to use ListView ? inserting custom widget is heavy and should not be done for many cells..
Whats your requirement ?

28th July 2010, 12:52
What I want is to build a list. The problem is that it is not a basic list.

Every element of this list must be composed of the layout previously described (some text on the left, in the middle and an icon on the right)

This is how a single element of the list should look like:

| text here ################################ text here ### ICON |
| other text here (might be in different style) ##### text here ### ICON |

The ICON spans the 2 rows.

So my idea was to build a widget representing a single element of the list and then add several widgets to the list.

Please tell me if something is still not clear.

Thanks again.

28th July 2010, 12:55
It would be better if you go for delegates then. See QItemDelegate
In the paint function you can calculate the rects yourself and draw text and place icons as per your requirement.

Other ways wont be as efficient as using delegates. Also see setItemData with which you can set different text you need..