Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pyqt QTreeWidget setColumnWidth不工作_Python_Qt_Pyqt_Qtreewidget - Fatal编程技术网

Python pyqt QTreeWidget setColumnWidth不工作

Python pyqt QTreeWidget setColumnWidth不工作,python,qt,pyqt,qtreewidget,Python,Qt,Pyqt,Qtreewidget,所以我在网上看到了一些类似的问题,但也许我不够聪明,无法将其应用于我的情况。在以下示例中,唯一受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.

所以我在网上看到了一些类似的问题,但也许我不够聪明,无法将其应用于我的情况。在以下示例中,唯一受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)

在您更新的示例中,每次触发resizeEvent时都会执行这一系列列调整,随着时间的推移,小部件调整大小的频率会非常高。这真的是你想要的吗?这还意味着,如果用户调整列大小,然后调整树的父级大小,则所有内容都将再次弹出到此大小


您可能希望将列选项移动到
showEvent
,以便在显示小部件时只执行一次。或者,不必设置显式大小,您也可以使用标题的最小大小设置。这实际上可能会强制执行最小大小,但仍然允许它们随大小调整而扩展。

没关系,我认为这是因为我在创建列之前调整大小,所以我现在在resizeEvent函数中进行调整,这似乎很有效。你真的希望在每个resizeEvent上都发生这种情况吗?随着时间的推移,当您积极调整大小时,这将触发。也许您希望在
showEvent
期间运行一次?