Search code examples
pythonpyqt4addchildqtreewidget

Python parent in QTreeWidget


I am working on python plugins.I used QTreeWidget to list items. [listing in qtreewidget][1] this link helped me a lot.

My code is:

valestimate=QTreeWidgetItem(str(parent_name))
for row in c.fetchall():
         strval=QTreeWidgetItem(unicode(row[0])) 
         valestimate.addChild(strval)
         self.treeWidget.addTopLevelItem((valestimate))

parent_name is name of my parent in QTreeWidget. EX: 'ACO_233' But output is :

![enter image description here][2]

If i set columncount as more then one then it is shown as:

![enter image description here][3]

How do i list full string as parent in Qtreewidget?? following this link [single character in qtreewidget][4] ..inserttoplevelitem takes list as parameter..But if i want to make any item as parent ,we cannot add list to qtreewidget type item. How do i do it??


Solution

  • Check the documentation:

     QTreeWidgetItem.__init__ (self, QStringList strings, int type = TreeWidgetItem.Type)
    

    Constructs a tree widget item of the specified type. The item must be inserted into a tree widget. The given list of strings will be set as the item text for each column in the item.

    QTreeWidgetItem expects a list of strings to fill in columns. When you do QTreeWidgetItem(str(parent_name)) it interprets the string parent_name as a list of characters (which is what a string is) and puts every character in a column. Instead you should be doing:

    valestimate = QTreeWidgetItem([parent_name])
    

    This will give you a single column item with parent_name as the value in that column.

    (By the way, str() or unicode() is not a good way to convert things around. You should use .encode to convert unicode into str or .decode for vice versa.)