Make QTreeView automatically resize to content

I am trying to make QTreeViewautomatically resized its contents. This is my setup: I'm trying to create a “smart” tool tip that is QWidgetwith Qt.Tool | Qt.FramelessWindowHinthow it WindowFlagsworks that works well. It contains a layout with some buttons (whose size is constant) and QTreeView. Now, when lines are added to the model QTreeView, it QTreeViewshould automatically increase its size to avoid vertical scrollbars.

The settings QSizePolicy.Expandingon the widget widgets QTreeViewdidn’t change anything - maybe I’m missing some additional flag or something like that.

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 precise, but I don’t know if this is the same or not), using this code, the view changes the correct dimensions (i.e., all content fits, and there would be no need for a strip scroll), but the scrollbar does not disappear, allowing you to scroll to empty space below the last line.

+5
source share
1 answer
0

All Articles