I am trying to make a QTreeView
automatically resize to its contents. This is my setup: I am trying to create a "smart" tool tip which is a QWidget
with Qt.Tool | Qt.FramelessWindowHint
as its WindowFlags
, which works nicely. It contains a layout with some buttons (the size of which is constant) and the QTreeView
. Now, whenever lines are added to the QTreeView
's model, the QTreeView
should automatically increase its size to avoid vertical scroll bars.
Setting QSizePolicy.Expanding
on both the tool tip widget and the QTreeView
did not change anything - maybe I am missing some additional flag or something here.
I also tried the following, brute-force method:
class ToolTip:
def __init__(self):
...
self.itemView.setVerticalScrollMode(QAbstractItemView.ScrollPerPixel)
self.itemView.verticalScrollBar().rangeChanged.connect(self.resizeViewVertically)
def resizeViewVertically(self, min, max):
self.resize(self.width(), self.height() + max)
which works most of the time, but not always. Every once in a while (every second time, to be exact, but I don't know if that is coincidence or not), using this code, the view resizes correctly (ie. all contents fit and there would be no need for the scroll bar), but the scroll bar does not disappear, allowing to scroll into the empty space below the last line.