所以我看到了一些类似的问题,但也许我不够聪明,无法将其应用于我的情况。在以下示例中,似乎受 setColumnWidth 影响的唯一列是第 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)
输出:250 0 0 0 0 0 0 0 0 0
我错过了什么?
更新:看起来我需要在 resizeEvent 函数中执行此操作,因为列已在init函数中设置:
''' 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)