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 resize QListView row to index widget size
QtWS25 Last Chance

How to resize QListView row to index widget size

Scheduled Pinned Locked Moved Solved General and Desktop
viewqlistviewsizehintrowresize
8 Posts 2 Posters 5.4k 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.
  • S Offline
    S Offline
    sm4ll-3gg
    wrote on last edited by
    #1

    I have custom model(ListModel), which contains AbstractModel*. From the AbstractModel are inherited 3 models : User, Group and Chat. Each of the models have own view (***View). I need to set my model(ListModel) and set for each item(index for QListView) own view. But when I set view (QListView::setIndexWidget) row height remains default.

    How to make the height of the row equal to the height of its view(views can resize dynamically)?

    QListView have sizeHintForRow method, but it never calls.
    From Qt docs: "Note that to control the height of a row, you must reimplement the QAbstractItemDelegate::sizeHint() function". But I can't get access to index widget from delegate.

    What I can do?

    VRoninV 1 Reply Last reply
    0
    • S sm4ll-3gg

      I have custom model(ListModel), which contains AbstractModel*. From the AbstractModel are inherited 3 models : User, Group and Chat. Each of the models have own view (***View). I need to set my model(ListModel) and set for each item(index for QListView) own view. But when I set view (QListView::setIndexWidget) row height remains default.

      How to make the height of the row equal to the height of its view(views can resize dynamically)?

      QListView have sizeHintForRow method, but it never calls.
      From Qt docs: "Note that to control the height of a row, you must reimplement the QAbstractItemDelegate::sizeHint() function". But I can't get access to index widget from delegate.

      What I can do?

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      @sm4ll-3gg said in How to resize QListView row to index widget size:

      QAbstractItemDelegate::sizeHint() function". But I can't get access to index widget from delegate.

      The index is the second argument of sizehint... http://doc.qt.io/qt-5/qabstractitemdelegate.html#sizeHint

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      S 1 Reply Last reply
      0
      • VRoninV VRonin

        @sm4ll-3gg said in How to resize QListView row to index widget size:

        QAbstractItemDelegate::sizeHint() function". But I can't get access to index widget from delegate.

        The index is the second argument of sizehint... http://doc.qt.io/qt-5/qabstractitemdelegate.html#sizeHint

        S Offline
        S Offline
        sm4ll-3gg
        wrote on last edited by sm4ll-3gg
        #3

        @VRonin yes ,I can get a model by index, but I can not get a view by index or model. I need view because I can get item widget only when I call view-> indexWidget(), isn't it?.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          Nope, the delegate replaces the indexWidget completely.

          What kind of widgets are you using? are they simple Qt widgets or are they composite?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          S 1 Reply Last reply
          0
          • VRoninV VRonin

            Nope, the delegate replaces the indexWidget completely.

            What kind of widgets are you using? are they simple Qt widgets or are they composite?

            S Offline
            S Offline
            sm4ll-3gg
            wrote on last edited by sm4ll-3gg
            #5

            @VRonin I'm using composite custom widgets. Yep, you right, delegate replaces indexWidget, but I don't understand how show my custom widget for display, not for edit.
            I.e., I need to all widgets are always visible, not only when edit requested

            Actually, I do not need editing at all. I need only show widgets and use them

            Please tell me how this can be done, if you know. Because I google it for 3 days. And I didn't find the right answer. The createEditor method shows the widget only for editing.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              It's not a trivial task. If your model is not huge you could use something like this: https://pastebin.com/XrppLZ3m but, in general, you'd have to manually handle the painting

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              S 1 Reply Last reply
              3
              • VRoninV VRonin

                It's not a trivial task. If your model is not huge you could use something like this: https://pastebin.com/XrppLZ3m but, in general, you'd have to manually handle the painting

                S Offline
                S Offline
                sm4ll-3gg
                wrote on last edited by sm4ll-3gg
                #7

                @VRonin Ty, it's works, what I should read to better understand Model / View / Delegate programming? I read all the documentaition on qt.io about this theme, but I did not see anything even remotely resembling such an approach.

                P.S. It is normal to set new delegate for each item if I need to show heterogeneous data in list?

                VRoninV 1 Reply Last reply
                0
                • S sm4ll-3gg

                  @VRonin Ty, it's works, what I should read to better understand Model / View / Delegate programming? I read all the documentaition on qt.io about this theme, but I did not see anything even remotely resembling such an approach.

                  P.S. It is normal to set new delegate for each item if I need to show heterogeneous data in list?

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  @sm4ll-3gg said in How to resize QListView row to index widget size:

                  what I should read to better understand Model / View / Delegate programming?

                  Basically everything I know comes from Advanced Qt Prgramming: Creating Great Software with C++ and Qt4

                  It is normal to set new delegate for each item if I need to show heterogeneous data in list?

                  No. What I would do is create a QStackedWidget with the possible combinations, use that as base and display the correct page based on the data in the index

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  3

                  • Login

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