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.
Re: Center items vertically in a QListView
Quote:
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?
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!