Results 1 to 3 of 3

Thread: Center items vertically in a QListView

  1. #1
    Join Date
    Mar 2018
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Center items vertically in a QListView

    Hi,

    I have a QListView with default settings (top to bottom flow). I'd like the items to be displayed centered vertically, i.e. in case they have a total height that's less than the height of the list, I'd like equal spacing to be displayed at the top and at the bottom.

    Is there a way to achieve that?

    Thanks.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Center items vertically in a QListView

    Is there a way to achieve that?
    Probably not without deriving a new class from QListView and reimplementing / overriding some methods. At a minimum, you'd probably need to override the resizeEvent(). In it, you can retrieve the count of the number of items, compute how much vertical space they take up (including the spacing()) and then, maybe, call QAbstractItemView::setViewportMargins() to set the top and bottom margins so the list is v-centered.

    Sort of a weird user interface, though, with a list position that keeps jumping up and down as items are added or removed. Have you really thought about how that will look and act in practice?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2018
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Center items vertically in a QListView

    It worked exactly the way you said. It seems like a weird interface, but it starts making sense if the list items are pages in a document viewer/editor. It works fine, thanks a lot!

Similar Threads

  1. QListView ~ Center items horizontally
    By scgrant327 in forum Qt Programming
    Replies: 17
    Last Post: 15th March 2016, 14:31
  2. Replies: 7
    Last Post: 9th September 2013, 09:31
  3. Center icon in QListView using QStyledItemDelegate
    By franki in forum Qt Programming
    Replies: 1
    Last Post: 11th June 2013, 09:53
  4. How to align list items to the center?
    By zgulser in forum Qt Tools
    Replies: 4
    Last Post: 9th February 2009, 10:52
  5. Q3listbox center items
    By sreedhar in forum Qt Programming
    Replies: 3
    Last Post: 24th February 2006, 12:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.