PDA

View Full Version : QSqlQueryModel and QTreeView



oddytz1989
21st February 2012, 11:38
Hello. Tell me how to use QSqlQueryModel and QTreeView do something like this?:
7420

Depending on the value of field2 to set the level of the nesting tree. That is, as shown in the picture. Record with id = 3 must be embedded into the record with id = 2, because the value of field2 = 2. and so on.

qlands
24th February 2012, 14:03
Hi,

I would:

1. Use QSqlQuery to get the the full data of that table organized by field1.
2. Browse through the records of the table. Each field1 will appear one after another and its parent field2 will be a previous field1
3. Use a QList<QTreeWidgetItem *> items; to hold the total amount of items.
4. Each record in the table will create a QTreeWidgetItem item.
4.1 If the record does not have field2 then add item to items with no parent and attach the field1 value to the item (you can use setData for this)
4.2 If the record has field2, search the current list of items looking for the value of field1, when fund use that item as parent for the new item the add it to items.
5. When its finished you can set the items to the treeview with: ui->mytree->insertTopLevelItems(0, items);

Good luck.
Carlos.