poonam
poonam

Reputation: 810

Python Query Result in QTreeWidget

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.

Upvotes: 0

Views: 1380

Answers (2)

Macbeth
Macbeth

Reputation: 31

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

top_node1 = QTreeWidgetItem([item_value])

Upvotes: 1

jdi
jdi

Reputation: 92569

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)

Upvotes: 2

Related Questions