2009-11-17 69 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