5

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.

rainer
  • 6,769
  • 3
  • 23
  • 37

1 Answers1

1

You may want to reference how this item works: http://ftp.ics.uci.edu/pub/centos0/ics-custom-build/BUILD/PyQt-x11-gpl-4.7.2/examples/layouts/flowlayout.py

May give you some hints as to what you need. Its a layout you can drop into a scrollwindow for isntance, and when the main window gets rsized, it wiill re-order all widgets inside to fit

Alex
  • 95
  • 1
  • 7