LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Qt Python : QTreeWidget Child Problem (https://www.linuxquestions.org/questions/programming-9/qt-python-qtreewidget-child-problem-769594/)

threaderslash 11-17-2009 12:28 AM

Qt Python : QTreeWidget Child Problem
 
Hello Everybody, :hattip:

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").

Code:

    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
Code:

    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: :eek:
Code:

        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. :D:D:D

threaderslash 11-17-2009 08:36 PM

Solved!
Here is the solution:
Code:

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. :D:D:D

Hope this may help others too.


All times are GMT -5. The time now is 11:32 PM.