Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stop the QTreeWidget from moving the scroll position?

I have a QTreeWidget with a bunch of QTreeWidgetItems. Each item has a couple columns. When one of the columns is wider than the width of the widget, there will be a scroll bar at the bottom. When I click on a QTreeWidgetItem inside the column that is wider than the widget, the QTreeWidget will automatically scroll to try and get as much of the column within the widget as possible. I do not want this to happen. How do I turn this off?

like image 739
Di Zou Avatar asked Sep 17 '25 09:09

Di Zou


1 Answers

This is behaviour is controlled by the QAbstractItemView.autoScroll property, which can be set like this:

treewidget.setAutoScroll(False)

However, this property is mainly used for automatically scrolling the tree widget when dragging items to the edge of the widget's viewport. So if this behaviour is still needed, it might be better to override the tree widget's mouse-press event, instead:

def mousePressEvent(self, event):
    self.setAutoScroll(False)
    QtGui.QTreeWidget.mousePressEvent(self, event)
    self.setAutoScroll(True)
like image 143
ekhumoro Avatar answered Sep 20 '25 01:09

ekhumoro