Easiest way to have a simple list with custom items?

  • Hi.

    I'm a bit lost with the amount of Qt classes for displaying views. I want something as simple as possible, hopefully as simple as a QListWidget, but I want to be able to display a little bit more than an icon and a label. I would need to display 3 rows of QLabels plus an image. More or less I would like to set a custom QWidget inside each element of the list.

    I don't need complex models at all, but I'm a little bit overwhelmed with so many possibilities. QStandardItemModel seems helpful, but it uses QStandardItems, and this class doesn't seem customizable for this purpose (I don't see how data/setData can be used for this).

    Do I need to create custom models and views for that, or is there a simpler approach?

    I've also thought of using a scroll area instead, since the kind of view is just a vertical list with about a dozen elements.

    Thank you.

  • Ok, when I was tagging the post I discovered "a similar question":http://qt-project.org/forums/viewthread/1478 were the "SpinBox delegate example":http://qt-project.org/doc/qt-4.8/itemviews-spinboxdelegate.html is mentioned. I looked at it and seems a step in the right direction, but not quite there: you can set a widget for editing, but I don't need that. It seems that one has to use paint events, etc.

    Still lost. :-(

  • Ok, I'm officially stupid, because in QListWidget there is setItemWidget, which does exactly this. Is not mentioned in the description, so I completely overlooked it. Just for the record, in case someone finds this at other time, here is a snippet:

    #include <QtGui>

    int main(int argc, char *argv[])
    QApplication a(argc, argv);

    QWidget window;

    QLabel* title = new QLabel("Custom widgets on a QListWidget");

    QListWidget* list = new QListWidget;

    QListWidgetItem* item;
    item = new QListWidgetItem(list);
    QPushButton* button = new QPushButton("hey");
    list->setItemWidget(item, button);


    QVBoxLayout* layout = new QVBoxLayout(&window);


    return a.exec();

  • Fantastic, I had a lot of work (and trouble) with trying to build this myself inside layouts with my own spacer etc.

    This is much better, works with any custom widget. Only one thing to add: The line "item->setSizeHint(button->minimumSizeHint());" sometimes doesn't work for custom widgets, dunno why. In that case just type: "item->setSizeHint(QSize(customWidget.width,customWidget.height));"

    And one last thought to share: If you just want a clean list top to bottom without actually choosing specific rows ( basically just using the custmWidget functionality, not the list functionality), then add:


    With this, you won't have the blue highlighting anymore when you click the area of any widgets/textareas. A nice way of just having customWidgets aligned vertically and also get scrollbars when needed.


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.