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,

Qt Code:
  1. QString parent_account = q.value(0).toString();
  2. QString account_name = q.value(3).toString();
  3.  
  4. if (prevClass != parent_account) {
  5. children = 0;
  6. parent = new QStandardItem(q.value(3).toString());
  7. m->appendRow(parent);
  8. prevClass = id;
  9. }else{
  10. QStandardItem *item = new QStandardItem(account_name);
  11. parent->setChild(children, item);
  12. ++children;
  13. }
To copy to clipboard, switch view to plain text mode 

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...