PDA

View Full Version : Center items vertically in a QListView



rsippl
16th March 2018, 15:34
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.

d_stranz
16th March 2018, 20:31
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?

rsippl
17th March 2018, 14:02
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!