0

So I saw some similar questions posted about this, but maybe I'm just not smart enough to apply it to my situation. In the following example, the only column that seems to be affected by the setColumnWidth is column 0.

''' A custom widget to set up the tree column widths '''

from PyQt4 import QtGui

class PCLTreeWidget(QtGui.QTreeWidget):
    def __init__(self,parent):
        QtGui.QTreeWidget.__init__(self,parent)

        ## set the sizes
        self.setColumnWidth(0,250)
        print self.columnWidth(0)
        self.setColumnWidth(1,250)
        print self.columnWidth(1)
        self.setColumnWidth(2,100)
        print self.columnWidth(2)
        self.setColumnWidth(3,1)
        print self.columnWidth(3)
        self.setColumnWidth(4,1)
        print self.columnWidth(4)
        self.setColumnWidth(5,3)
        print self.columnWidth(5)
        self.setColumnWidth(6,1)
        print self.columnWidth(6)
        self.setColumnWidth(7,1)
        print self.columnWidth(7)
        self.setColumnWidth(8,1)
        print self.columnWidth(8)
        self.setColumnWidth(9,1)
        print self.columnWidth(9)

Output: 250 0 0 0 0 0 0 0 0 0

What am I missing?

update: looks like I need to do it in the resizeEvent function since the columns are setup yet in the init function:

''' A custom widget to set up the tree column widths '''
  2
  3 from PyQt4 import QtGui
  4
  5 class PCLTreeWidget(QtGui.QTreeWidget):
  6     def __init__(self,parent):
  7         QtGui.QTreeWidget.__init__(self,parent)
  8
  9
 10     def resizeEvent(self, resizeEvent):
 11         ## handle resize
 12         self.setColumnWidth(0,250)
 13         self.setColumnWidth(1,250)
 14         self.setColumnWidth(2,30)
 15         self.setColumnWidth(3,60)
 16         self.setColumnWidth(4,50)
 17         self.setColumnWidth(5,250)
 18         self.setColumnWidth(6,35)
 19         self.setColumnWidth(7,45)
 20         self.setColumnWidth(8,60)
 21         self.setColumnWidth(9,35)
 22         self.header().setResizeMode(0,QtGui.QHeaderView.Stretch | QtGui.QHeaderView.Interactive)
 23         self.header().setResizeMode(1,QtGui.QHeaderView.Stretch)
 24         self.header().setResizeMode(2,QtGui.QHeaderView.Fixed)
 25         self.header().setResizeMode(3,QtGui.QHeaderView.Fixed)
 26         self.header().setResizeMode(4,QtGui.QHeaderView.Fixed)
 27         self.header().setResizeMode(5,QtGui.QHeaderView.Stretch)
 28         self.header().setResizeMode(6,QtGui.QHeaderView.Fixed)
 29         self.header().setResizeMode(7,QtGui.QHeaderView.Fixed)
 30         self.header().setResizeMode(8,QtGui.QHeaderView.Fixed)
 31         self.header().setResizeMode(9,QtGui.QHeaderView.Fixed)
user1313404
  • 29
  • 1
  • 4
  • Never mind, I think it is because I am resizing before the columns are created, so I'm doing it in the resizeEvent function now and that seems to work – user1313404 Apr 20 '12 at 18:35
  • Do you really want this to happen on every resizeEvent? This is going to fire as you are actively resizing over time. Maybe you want this to be run once during the `showEvent`? – jdi Apr 20 '12 at 18:57

1 Answers1

1

In your updated example, this series of column adjustments is going to be executed every time a resizeEvent fires, which can be extremely often as the widget resizes over time. Is this really what you want? It would also mean that if a user adjusts the column sizing, and then resizes the parent of the Tree, everything will pop back to this sizing again.

You might want to move the column options to a showEvent instead so they are only done once when the widget is shown. Or, instead of setting the explicit sizes, maybe you could play around with the minimum size setting for the headers. http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qheaderview.html#setMinimumSectionSize. That may actually enforce your min sizes, but still allow them to expand with resizes.

jdi
  • 90,542
  • 19
  • 167
  • 203