Qt World Summit: Submit your Presentation

How to use custom widgets in a QListView?

  • Is there any way to use custom widgets in a QListView to represent each item in the list? There are some examples on how to use delegates to setup editing widgets and to use custom painters, but I haven't come across any example showcasing how to use custom widgets to display data in a view. Does Qt offer any way to setup a QListView (or any view) to use custom widgets to represent each item?

  • Lifetime Qt Champion

    The delegates are designed for that. Also to allow easy editing and saving.

    Also why do u want a View and then custom widgets ?
    it will completely ruin editing and most of the benefits of the View+model.

    Why not just ListWidget then and setItemWidget

    WARNING: this get heavy with many items and consumes massive amount of mem.

    Alternately just use a scrollarea.

    So it all comes down to.
    How many items u must have and how powerful your device is.
    As a list of full blown widgets kill phones pretty fast but my
    dev box took 300.000 before it got a bit tired.

    Using setItemWidget is only useful for few items and it will often
    lag when scrolling etc.
    So if you have many items, Delegates are the way to go.

  • @mrjj although delegates appear to be designed for that, my point was that all examples cover only setting up the editing widget and painting directly in the paint area. Although setting up a widget to render the data appears to be a basic usecase, no example covers this particular usecase.

    And regarding why I or anyone would want to represent data in widgets designed to represent data, that's pretty much a basic usecase. This is a given in some UI toolkits, such as javafx or Android.

    About using setItemWidget(), that is used to cover the very specific corner case of having to set a specific widget instance at a specific index. I'm looking for a way to configure a View to use a common widget by providing a Delegate, which appears to be the recommendation included in the doc section on setItemWidget().

  • Lifetime Qt Champion

    Delegates draw the widgets with paint for performance reasons.
    Only when editing, real widgets are used. This is pr design.
    So it does use real widgets when in edit mode but expects the Delegate to use
    QStyle to paint exact looking Widgets using QStyle .
    Its more work but give good performance.

    For reference on how to paint the widgets, i often just look in Qt source.
    Most of the time its just setting up an Options struct and call some drawComplexObject in QStyle. You can also cheat and use render() if you have a lots of widgets in one cell. But that uses memory since each items then has an image to maintain.

    That said, if you not having many items. setItemWidget or scrollarea will work just fine too.

    Could you should a picture of the View with the wanted widgets?
    maybe we can give some suggests.

Log in to reply