Qt Python:QTreeWidget Child Problem

Thr*_*ash 1 user-interface qt qt4 pyqt pyqt4

如果我的树形图上只有一个级别,我有一个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)
Run Code Online (Sandbox Code Playgroud)

这是输出,当它工作

def eqpt_activateInput(self,item,col):
    print "Qtree ok! pressed"
    print item.text(col)
Run Code Online (Sandbox Code Playgroud)

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

    AttributeError: 'NoneType' object has no attribute 'setText'
Run Code Online (Sandbox Code Playgroud)

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

任何提示或建议都非常感谢.

Ida*_*n K 5

您的树节点如下所示:

Node 1
   Node 1.1
      Node 1.1.1
Node 2
Run Code Online (Sandbox Code Playgroud)

(请原谅我糟糕的演讲)

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

#child 2 - from father 1
self.colorTreeWidget.topLevelItem(0).child(1)...
Run Code Online (Sandbox Code Playgroud)

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

一般来说,我不会以这种方式构建你的树,你可以看到它有多混乱,而这:

parent = QtGui.QTreeWidgetItem(self.colorTreeWidget)
firstchild = QtGui.QTreeWidgetItem(parent)
secondchild = QtGui.QTreeWidgetItem(parent)
parent = QtGui.QTreeWidgetItem(self.colorTreeWidget)
Run Code Online (Sandbox Code Playgroud)

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

甚至这个:

parent = QtGui.QTreeWidgetItem(self.colorTreeWidget)
parent.addChild(...)
parent.addChild(...)
Run Code Online (Sandbox Code Playgroud)