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 automatically adjust the size of QListView?
Forum Updated to NodeBB v4.3 + New Features

How to automatically adjust the size of QListView?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.9k Views 2 Watching
  • 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.
  • A Offline
    A Offline
    Asquator
    wrote on last edited by Asquator
    #1

    I'm coding a Qt Widgets desktop app and I have a simple QListView with a custom model and a delegate. I want the QListWidget size to resize automatically when new elements are added to it, such that in the following image, the + button appears just below the last QLineEdit delegate item.
    qlist.png

    There is a vertical layout that owns the list and a button.

    I tried reimplementing the sizeHint function according to this topic: (tried different answers)
    https://stackoverflow.com/questions/25613456/adjust-the-height-of-qlistview-to-fit-the-content

    I also called updateGeometry() on the view, but nothing of the above worked for me. Even If I return a constant value in sizeHint, the size doesn't change.

    Any direction, please?

    P.S I tried to look for similar questions on Qt forum, but found no solution so far

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Do you mean you want your QListView to expand so that all items are always visible ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Do you mean you want your QListView to expand so that all items are always visible ?

        A Offline
        A Offline
        Asquator
        wrote on last edited by
        #3

        Do you mean you want your QListView to expand so that all items are always visible ?

        I want my QListView to always fit size of it's actual contents, for example if the list has two rows, the height should be exactly sufficient to contain these two rows and no more. If I add elements to the model or remove them, I expect the widget to be resized respectively to the number of rows.

        (I want the button to be just below the last row and move with new rows added to the list), and I wonder if I even need to play with the list size.

        Thank you in advance!

        SGaistS 1 Reply Last reply
        0
        • A Asquator

          Do you mean you want your QListView to expand so that all items are always visible ?

          I want my QListView to always fit size of it's actual contents, for example if the list has two rows, the height should be exactly sufficient to contain these two rows and no more. If I add elements to the model or remove them, I expect the widget to be resized respectively to the number of rows.

          (I want the button to be just below the last row and move with new rows added to the list), and I wonder if I even need to play with the list size.

          Thank you in advance!

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Since the button is under the list view, I suspect you are using a QVBoxLayout, did you add a stretch after the button so that everything is pushed to the top ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          1
          • SGaistS SGaist

            Since the button is under the list view, I suspect you are using a QVBoxLayout, did you add a stretch after the button so that everything is pushed to the top ?

            A Offline
            A Offline
            Asquator
            wrote on last edited by
            #5

            Thank you, the issue was solved by changing the stretch factors.
            It was enough to change
            listLayout->addWidget(button);
            to
            listLayout->addWidget(button,1,Qt::AlignTop);
            So the stretch factor ratio 1:0 allows to allign the button on the top of it's area.

            1 Reply Last reply
            1
            • A Asquator has marked this topic as solved on

            • Login

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