开发者

Qt Python : QTreeWidget Child Problem

开发者 https://www.devze.com 2022-12-11 20:53 出处:网络
I have a QTreewidget that works fine if I have just one level on my treelist. If I decide to add child sublevels, it gives me an error. Here is the code, that works nice only without the \"childs\" li

I have a QTreewidget that works fine if I have just one level on my treelist. If I decide to add child sublevels, it gives me an error. Here is the code, that works nice only without the "childs" lines on it (see after "child 1" and "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)

Here is the output, when it works

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

if I exclude the lines related to the "child 1" and "child 2" from the code, it runs. Otherwise, it gives me the error:

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

I used 开发者_开发知识库the Qt Designer to generate the code, and added some lines to trigger events.

Any hints or suggestions are highly appreciated.


Your tree nodes look like this:

Node 1
   Node 1.1
      Node 1.1.1
Node 2

(forgive me for the poor presentation)

In your code you're accessing the first top level node's second child:

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

but it doesn't exist, since you mistakenly (I assume) added the child for the wrong node.

In general I wouldn't build your tree that way, you can see how confusing it gets, while this:

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

with unique names for each node is much more clear.

Or even this:

parent = QtGui.QTreeWidgetItem(self.colorTreeWidget)
parent.addChild(...)
parent.addChild(...)


Solved! Here is the solution:

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

Now it works properly.

Thanks again for the suggestions and comments!

0

精彩评论

暂无评论...
验证码 换一张
取 消