PDA

View Full Version : How to save the content from a QTreeWidget (tree structure, model) to database



whitefurrows
11th May 2011, 16:43
Hi

how can i save and load the items for a QTreeWidget(TreeModel) line by line. My current table looks like this(ListModel):


CREATE TABLE tree
(
ID integer NOT NULL PRIMARY KEY AUTOINCREMENT,
entries varchar (50) NOT NULL
);

and i load it by

QTreeWidgetItem* item;
for (int i=0; i<allEntries.size(); i++)
{
item=new QTreeWidgetItem(ui.treeWidget);
item->setText(0, allEntries.at(i).getName());
}

Now i want add a field (index) to my database, and store the the current TreeModel-Index within to add rows under a row. How can i do that?

wysota
12th May 2011, 06:51
Iterate over the tree and run an INSERT statement for each item you traverse.

whitefurrows
12th May 2011, 09:22
What is the value(index) for my INSERT statement if i iterate over the tree?


ui.treeWidget->selectionModel()->currentIndex().row()
I think thats not the right value if my model looks like this:
http://www.loaditup.de/files/612893.png

Should i use nested sets-modell and is that easy to handle with a QTreeWidget?

wysota
12th May 2011, 11:20
You surely can't save a hierarchical model into a database that doesn't provide any means to store information about the relationship between items. Furthermore it seems your "tree" is "flat", based on the code snippet you provided. Anyway, the minimal sql table needed to store hierarchical data consists of three columns -- id of the element, data of the element and id of the parent of the element. Then it's just a matter of recursing into the tree and storing that information in the database.

whitefurrows
12th May 2011, 15:04
Shold i store the id of the element, data of the element and id of the parent of the element into one database field or into three?

What is the common way to load the tree?

wysota
12th May 2011, 23:59
Shold i store the id of the element, data of the element and id of the parent of the element into one database field or into three?
If you are asking this question then I suggest you get familiar with the concept or relational databases before trying to use one.

whitefurrows
20th May 2011, 17:56
Hi,

sorry for my bad english, but i have understand the concept of relational databases. I have start my first try to create a tree structure. It looks good but only one problem exist. The tree should look like this:
http://www.qtcentre.org/attachment.php?attachmentid=6458&d=1305924133

but that is the result

http://www.qtcentre.org/attachment.php?attachmentid=6457&d=1305924131

from this table

http://www.qtcentre.org/attachment.php?attachmentid=6456&d=1305924130

The tree was build with this function

addTree(0);

void MyWidget::addTree(const int id)
{
QList<Entries*> folders = db->getEntries(id);
MyTreeWidgetItem* item;

foreach(Entries* obj, folders)
{
if(obj->getParentID())
{
item=new MyTreeWidgetItem(treeWidget->currentItem());
}
else
{
item=new MyTreeWidgetItem(treeWidget);
}

item->setEntries(obj);
item->setText(0, obj->getName());
item->setIcon(0, QIcon(":/folder.ico") );
treeWidget->setCurrentItem(item);

addNodes(obj->getID());
}
}

I think the problem is i set every time the current item to the QTreeWidget. I don't known what the right criteria to do that. Can you help me please?