PDA

View Full Version : How to find which QTreeWidgetItem?



aekilic
30th September 2009, 09:57
Dear All

We create a QTreeWidget with a SQL query. We bring all the values like


QSqlQuery q;
q.exec("...");
while(q.next())
{
QTreeWidgetItem *cities = new QTreeWidgetItem(treeWidget);
cities->setText(0, tr(q.value(0).toString()));
}


There is no problem with this one. But in the query in some row I have child on cities items.

q.value(0).toString() is not same in every row, so is it possible to find which item on the tree that I could add the child?

wysota
30th September 2009, 11:00
You have to find the proper item based on the result of your query. Or maybe I don't understand the problem...

aekilic
30th September 2009, 11:23
problem is like this,
query is like this
1 New York
2 Helena
3 Salt Lake City

the tree is like this
id city
1 New York
2 Helena
3 Salt Lake City

the three is like this. but in the query I get a row like
2.1 xxx
2.2 yyy which I had to show

2 Helena
2.1 xxx
2.2 yyy

Or could be 2.2.1 in the query...

Were I able to explain?

wysota
30th September 2009, 11:41
Were I able to explain?

Not really. I know what a tree is, I don't know what you are having problems with.

aekilic
30th September 2009, 17:00
query is like this
1 New York
2 Helena
3 Salt Lake City
2.1 xxx 2
3.1 uuu 3
1.1 iiii 1
2.2 yyy 2
3.1.1 yuyu 3.1

I know from the query what is the parent but I dont know how can I put it under parent in the tree in a while(q.next)

wysota
30th September 2009, 17:15
Pass a pointer to the parent item to the constructor of the child item instead of a pointer to the tree widget.

aekilic
30th September 2009, 21:33
Normaly in table we could put a new row and a item like

insertRow, setitem...etc

is there a possible way to insert a tree like that?

wysota
30th September 2009, 22:02
There is QTreeWidgetItem::insertChild(), if it makes a difference for you not to pass the parent into the constructor of the child...

aekilic
30th September 2009, 22:16
then how can I find the index?

wysota
30th September 2009, 22:19
index of what? The child? You can use addChild() instead of insertChild() to append it to the end of the parent's child list.

Come on man... open the docs and at least browse through the API...

aekilic
30th September 2009, 22:37
I think I was not able to explain,

It so clear that I create the tree

1
2
3
4


thats is ok. And from the query I get values like
1.2
1.1
4.1
1.1.1
1.1.4
1.1.2
1.1.3
not with an order. What I am planing is when I get a value like(4.1) this, I should find the index for 4 and addChild for this item?

The main problem is when I create the treewidget, we put every item with the same name. So after if you would like to add a child every item has the same name, so where to put? As I have said I should find 4 first ( that is my main problem) and after that i could either insert or add a child. And that is why I have asked you how can I find the index of the item

I am looking for the docs since morning but wasnt able find something helpfull.

wysota
1st October 2009, 08:22
I am looking for the docs since morning but wasnt able find something helpfull.

You should look more carefully then.

QTreeWidget::findItems() to find by content, QTreeWidget::topLevelItem() to find by index.

aekilic
1st October 2009, 11:54
http://qt.nokia.com/developer/faqs/762