1

如果我的树列表中只有一个级别,我有一个 QTreewidget 可以正常工作。如果我决定添加子子级别,它会给我一个错误。这是代码,只有在没有“childs”行的情况下才能正常工作(参见“child 1”和“child 2”之后)。

def eqpt_centralwdg(self,MainWindow):
    self.centralwidget = QtGui.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")

    self.colorTreeWidget = QtGui.QTreeWidget(self.centralwidget)
    self.colorTreeWidget.setGeometry(QtCore.QRect(60, 60, 191, 141))
    self.colorTreeWidget.setObjectName("colorTreeWidget")

    # father root 1
    item = QtGui.QTreeWidgetItem(self.colorTreeWidget)
    #child 1 - from father 1
    item = QtGui.QTreeWidgetItem(item)
    #child 2 - from father 1
    item = QtGui.QTreeWidgetItem(item)
    # father root 2
    item = QtGui.QTreeWidgetItem(self.colorTreeWidget)         

    self.connect(self.colorTreeWidget, QtCore.SIGNAL('itemClicked(QTreeWidgetItem*, int)'), self.eqpt_activateInput)

    MainWindow.setCentralWidget(self.centralwidget)

def eqpt_retranslateUi(self, MainWindow):
    MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)
    self.colorTreeWidget.headerItem().setText(0, QtGui.QApplication.translate("MainWindow", "color", None, QtGui.QApplication.UnicodeUTF8) 
    __sortingEnabled = self.colorTreeWidget.isSortingEnabled()    
    self.colorTreeWidget.setSortingEnabled(False) 
    # father root 1
    self.colorTreeWidget.topLevelItem(0).setText(0, QtGui.QApplication.translate("MainWindow", "Yellow", None, QtGui.QApplication.UnicodeUTF8) 
    #child 1 - from father 1
    self.colorTreeWidget.topLevelItem(0).child(0).setText(0, QtGui.QApplication.translate("MainWindow", "Yellow Sun", None, QtGui.QApplication.UnicodeUTF8))
    #child 2 - from father 1
    self.colorTreeWidget.topLevelItem(0).child(1).setText(0, QtGui.QApplication.translate("MainWindow", "Yellow Gold", None, QtGui.QApplication.UnicodeUTF8))        

    # father root 2
    self.colorTreeWidget.topLevelItem(1).setText(0, QtGui.QApplication.translate("MainWindow", "Blue", None, QtGui.QApplication.UnicodeUTF8) 

    self.colorTreeWidget.setSortingEnabled(__sortingEnabled)

这是输出,当它工作时

def eqpt_activateInput(self,item,col):
    print "Qtree ok! pressed"
    print item.text(col)

如果我从代码中排除与“child 1”和“child 2”相关的行,它就会运行。否则,它会给我错误:

    AttributeError: 'NoneType' object has no attribute 'setText'

我使用 Qt Designer 生成代码,并添加了一些行来触发事件。

任何提示或建议都将受到高度赞赏。

4

2 回答 2

5

您的树节点如下所示:

Node 1
   Node 1.1
      Node 1.1.1
Node 2

(请原谅我的糟糕介绍)

在您的代码中,您正在访问第一个顶级节点的第二个子节点:

#child 2 - from father 1
self.colorTreeWidget.topLevelItem(0).child(1)...

但它不存在,因为您错误地(我假设)为错误的节点添加了子节点。

一般来说,我不会那样构建你的树,你可以看到它变得多么混乱,而这个:

parent = QtGui.QTreeWidgetItem(self.colorTreeWidget)
firstchild = QtGui.QTreeWidgetItem(parent)
secondchild = QtGui.QTreeWidgetItem(parent)
parent = QtGui.QTreeWidgetItem(self.colorTreeWidget)

每个节点的唯一名称更加清晰。

甚至这样:

parent = QtGui.QTreeWidgetItem(self.colorTreeWidget)
parent.addChild(...)
parent.addChild(...)
于 2009-11-17T11:50:09.853 回答
0

解决了!这是解决方案:

    parent1 = QtGui.QTreeWidgetItem(self.colorTreeWidget)
    child1_1 = QtGui.QTreeWidgetItem()
    child1_2 = QtGui.QTreeWidgetItem()
    parent1.addChild(child1_1)
    parent1.addChild(child1_2)

现在它可以正常工作了。

再次感谢您的建议和意见!

于 2009-11-18T02:33:49.673 回答