Qt World Summit: Register Today!

How to create a listbox with custom listbox items?

  • I need to create a (vertical) listbox of custom listbox items (a frame around 2 push buttons and a label). I'm having trouble doing this with QListWidget as it needs a QListWidget item, which itself can't draw my special grouping of UI widgets (2 buttons and a label). I ended up creating a sub-class of QListWidgetItem and have it also inherit from QFrame (because my UI is a frame with 2 buttons and a label inside it) so I can initialize my UI (via setupUI). However, QListWidget only offers ViewModes ListMode and Icon Mode, which I don't believe will work for my scenario.

    Can anyone suggest a better way to setup a listbox with custom listbox items like I want? I tried to search through forums for something similar but didn't find it so apologies if there is an existing thread out there.


  • Would QML and a ListView be an acceptable solution?

    Another alternative is a custom delegate with QListView.

    Or how about a simple QVBoxLayout inside a QScrollArea?

  • Thanks for the suggestions, ZapB. I'm not familiar with QML so not sure if that is a good choice.

    I'm reading up on the custom delegate with QListView method now. The alternative of a QVBoxLayout in a scroll area is more appealing because there's not much of a learning curve there but then I would have to implement my own listbox class.

    Just to reiterate, I need to create a list box with custom list box items. The listbox items will consist of a frame around 2 push buttons and a label so I will need to override the input. There can be up to 256 of these items in a list box and users will be able to add, remove and select them freely. I'll probably need to incorporate a context menu for the "selected" item. Also, I will need to be able to support drag into these list box items.

    So with the above info, do you know if there is a better choice between the last 2 options? Any unnecessary overhead with one vs the other, etc.?

    If I go with the delegate solution, would QItemDelegate sub-class for the list box items be the way to go? What kind model do I setup - standard? Sorry, I'm not terribly familiar with how model/view system works (which is why the last suggestion was appealing).

    Thanks again for your help.

  • For QML it depends on how much you want your items to match the system theme. QML is probably the easiest option but there are not yet pre-defined desktop components available. However, it is very easy to do and to add animations etc. It also comes with support for current item highlighting etc.

    If QML is a no-go for you then I would go for the custom delegate approach I think since 256 is quite a lot of widgets to display at once. Using a delegate, the QListView will handle creation and deletion of the delegates for you.

    To see how ot use your own delegate take a look at this "example":http://doc.qt.nokia.com/latest/itemviews-stardelegate.html. How you map your model onto the delegate is up to you but normally you would provide a different Role for each piece of data to be shown within a single delegate.


  • Thanks again, ZapB!

Log in to reply