2009-11-17 70 views
1

我有一个QTreewidget,如果我在我的treelist上只有一个级别,它可以正常工作。如果我决定添加子级子文件,它会给我一个错误。这里是代码,只有在没有“孩子”行的情况下才能很好地工作(参见“孩子1”和“孩子2”之后)。Qt Python:QTreeWidget儿童问题

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) 

如果我排除有关“孩子1”和“2儿”从代码行,它运行。否则,它给我的错误:

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

我用Qt设计器生成的代码,并添加了一些行来触发事件。

任何提示或建议,高度赞赏。

回答

0

解决了! 这里是解决方案:

parent1 = QtGui.QTreeWidgetItem(self.colorTreeWidget) 
    child1_1 = QtGui.QTreeWidgetItem() 
    child1_2 = QtGui.QTreeWidgetItem() 
    parent1.addChild(child1_1) 
    parent1.addChild(child1_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(...) 
+0

嗨,丹尼尔。感谢您的反馈意见。它有助于解决错误。 – ThreaderSlash 2009-11-17 23:22:02