Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use custom widgets in a QListView?
QtWS25 Last Chance

How to use custom widgets in a QListView?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 7.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rmam
    wrote on 26 Apr 2018, 09:51 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 26 Apr 2018, 10:57 last edited by mrjj
      #2

      Hi
      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
      http://doc.qt.io/qt-5/qlistwidget.html#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.

      R 1 Reply Last reply 27 Apr 2018, 07:15
      3
      • M mrjj
        26 Apr 2018, 10:57

        Hi
        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
        http://doc.qt.io/qt-5/qlistwidget.html#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.

        R Offline
        R Offline
        rmam
        wrote on 27 Apr 2018, 07:15 last edited by rmam
        #3

        @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().

        M 1 Reply Last reply 27 Apr 2018, 08:04
        0
        • R rmam
          27 Apr 2018, 07:15

          @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().

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 27 Apr 2018, 08:04 last edited by mrjj
          #4

          Hi
          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.

          1 Reply Last reply
          1

          2/4

          26 Apr 2018, 10:57

          • Login

          • Login or register to search.
          2 out of 4
          • First post
            2/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved