tags:

views:

307

answers:

3

Hello Everybody,

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.

+3  A: 

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(...)
Idan K
Hi Daniel. Thanks for your feedback. It helped to fix the error.
ThreaderSlash
A: 

My child1 and child2 is not displaying in my tree when I run the program.

instead of showing

Parent 1 "Yellow"
    child 1 "Yellow Sun"
    child 2 "Yellow Gold"
Parent 2 "Blue"

It just shows the Parents on the tree:

Parent 1 "Yellow"
Parent 2 "Blue"

Here is the new version of the code:

# parent 1
parent1 = QtGui.QTreeWidgetItem(self.colorTreeWidget)
child1_1 = QtGui.QTreeWidgetItem(parent1)
child1_2 = QtGui.QTreeWidgetItem(parent1)
# parent 2
parent2 = QtGui.QTreeWidgetItem(self.colorTreeWidget)
....
# parent 1
self.colorTreeWidget.topLevelItem(0).setText(0, QtGui.QApplication.translate("MainWindow", "Yellow", None, QtGui.QApplication.UnicodeUTF8) 
#child 1 - from parent 1
self.colorTreeWidget.topLevelItem(0).child(0).setText(0, QtGui.QApplication.translate("MainWindow", "Yellow Sun", None, QtGui.QApplication.UnicodeUTF8))
#child 2 - from parent 1
self.colorTreeWidget.topLevelItem(0).child(1).setText(0, QtGui.QApplication.translate("MainWindow", "Yellow Gold", None, QtGui.QApplication.UnicodeUTF8))        

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

Any hints or suggestion?

All comments are highly welcome and appreciated.

ThreaderSlash
A: 

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!

ThreaderSlash