Getting rid of blank area at the bottom of a QListWidget
Asked Answered
F

2

5

I'm using QListWidget with setItemWidget and when adding items to the list and scrolling down to the bottom I get this result:

enter image description here

So there's an area which is completely blank at the bottom (not selectable, in the image above the last item in the list has been selected)

How can I get rid of this blank area at the bottom of the list?

Please note that the height of the rows can be different within a list

Grateful for help and with kind regards, Tord

Forefront answered 24/1, 2017 at 11:50 Comment(0)
F
6

It's possible to change the type of vertical scrolling that is done for list by using setVerticalScrollMode for the QListWidget so that the scrolling is done per pixel (instead of per item which is the default):

self.list_widget.setVerticalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)

The result is that rows that are in the top-most part of the QListWidget box can be drawn only in part, which in turn means that the blank area that exists when scrolling per item will disappear:

enter image description here

Reference documentation: https://doc.qt.io/qt-5/qabstractitemview.html#verticalScrollMode-prop

Forefront answered 25/1, 2017 at 17:14 Comment(4)
I had a look for something like this yesterday, but couldn't find what I was looking for. Once you look at things in terms of scrolling rather than items, it's obvious what the solution should be - so I'm quite disappointed I didn't come up with this myself :|Withdrew
Nice solution :) I may try this one if it fits my needs, do you know if there are some performance issues depending on the number of items in the QListWidget comparing to the default scroll mode ?Sumerology
@SyedElec I'm not sure but I don't think so, they don't write anything about performance here so I'm hoping it won't have any negative effectForefront
@Forefront Ok Thank you, it works well also with a QListView.Sumerology
S
1

I'm using exactly the same approach and I get rid of the blank area by creating a widget that contains my QListWidget and use :

listwidget.setFixedSize(widget.width(), listwidget.sizeHintForRow(0) * listwidget.count() + 2 * listwidget.frameWidth())

listwidget is you QListWidget object

widget is the widget that contains your QListWidget object

Sumerology answered 24/1, 2017 at 18:4 Comment(1)
Thank you for the help, I'm not sure that it will work for my situation though since the rows can have different heights (I have now updated the question to make this clear)Forefront

© 2022 - 2024 — McMap. All rights reserved.