PDA

View Full Version : QTreeView with QStandardItemModel



aekilic
16th February 2011, 09:11
Dear All

I try to fill a tree with a sql query help of QStandardItemModel.

My query result is like below.

parent_account id name
-1 438 a
-1 439 b
439 478 c
478 479 d
478 480 e
-1 440 f
440 465 g
440 466 h
440 467 i
440 468 j
440 469 k
-1 441 l

My tree should look like

a
b
-c
--d
--e
f
-g
-h
-i
-j
-k
l

I try to put the values like this,



QString parent_account = q.value(0).toString();
QString account_name = q.value(3).toString();

if (prevClass != parent_account) {
children = 0;
parent = new QStandardItem(q.value(3).toString());
m->appendRow(parent);
prevClass = id;
}else{
QStandardItem *item = new QStandardItem(account_name);
parent->setChild(children, item);
++children;
}


When I have this code i could only get a one child. The problem is in the sql query there could be lot of child item. Can any one help me how to do this...