0

I am working on python plugins.I used PyQt4 Designer. I want to list query result into QTreeWidget. My code is as follows:

c = self.db.con.cursor()
self.db._exec_sql(c, "select est from bio")
for row in c.fetchall():
    item_value=unicode(row[0])
    top_node1 = QTreeWidgetItem(item_value)
    self.treeWidget.insertTopLevelItem(0, top_node1)

The query returns the values as:

enter image description here

But when i list these values into QTreeWidget using above code,it is shown as below :

enter image description here

Only first character is shown.If i change '0' to some other number in self.treeWidget.insertTopLevelItem(0, top_node1) ,nothing appears in QTreeWidget.

How do i do it???? thanx.

jdi
  • 90,542
  • 19
  • 167
  • 203
poonam
  • 748
  • 4
  • 19
  • 40

2 Answers2

2

If you take a look at the documentation for a QTreeWidgetItem, you will see there are a number of possible constructors for creating an instance. Though none of which it seems you are using in a way that is going to give you desirable results. The closest match to the signature you are providing is:

QTreeWidgetItem ( const QStringList & strings, int type = Type )

What this is probably doing is taking your string (I am assuming row[0] is a string because I don't know which drivers you are using) and applying it as a sequence, which would fullfill the requiremets of QStringList. Thus what you are getting is populating multiple columns of your item with each letter of your string value. If this is what you wanted, then you would n eed to tell your widget to show more columns: self.treeWidget.setColumnCount(10). But this isn't what you are looking for I am sure.

More likely what you should be trying is to create a new item, then add the value to the desired column:

item = QTreeWidgetItem()
item.setText(0, unicode(row[0]))
self.treeWidget.insertTopLevelItem(0, item)

You can use the default constructor with no arguments, set the text value of the first column to your database record field value, and then add that item to the tree. You could also build up a list of the items and add them at once:

items = []
for row in c.fetchall():
    item = QTreeWidgetItem()
    item.setText(0, unicode(row[0]))
    items.append(item)
self.treeWidget.insertTopLevelItems(0, items)
jdi
  • 90,542
  • 19
  • 167
  • 203
  • Thanx..:)..How do i make one item as parent of tree...??? I tried valestimate=QTreeWidgetItem(sel_estimate) self.treeWidget.__init__ (self,valestimate,items, type = QTreeWidgetItem.Type)...but it gives error. – poonam Jul 09 '12 at 06:22
  • 1
    Are you trying to call the init method on the tree? Dont do that. The only time that init method should be used directly is when you subclass the treewidget to make a custom one. QTreeWidgetItem has its own methods for adding children. Just create a new item with no args. Set it up. Then do parentItem.addChild(childItem). And very importand: read the docs http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qtreewidgetitem.html – jdi Jul 09 '12 at 14:57
1

Your first aproach could be corrected just add a list to the widgetitem not a string like this:

top_node1 = QTreeWidgetItem([item_value])

Macbeth
  • 31
  • 2
  • for top level items use: self.treeWidget.addTopLevelItems(items) – Macbeth Aug 03 '12 at 22:40
  • Welcome to Stack Overflow! Please have a look at [how you can format your code](http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) and what [other options](http://stackoverflow.com/editing-help/) you have to format your posts. Note that you can edit your own posts to add information that you forgot. I recommend you skim through [this list of FAQs](http://meta.stackexchange.com/questions/7931/faq-for-stack-exchange-sites), and read some of them. Especially about answering and asking questions. – phant0m Aug 04 '12 at 14:37