How to save the content from a QTreeWidget (tree structure, model) to database
Hi
how can i save and load the items for a QTreeWidget(TreeModel) line by line. My current table looks like this(ListModel):
Code:
CREATE TABLE tree
(
ID integer NOT NULL PRIMARY KEY AUTOINCREMENT,
entries varchar (50) NOT NULL
);
and i load it by
Code:
for (int i=0; i<allEntries.size(); i++)
{
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?
Re: How to save the content from a QTreeWidget (tree structure, model) to database
Iterate over the tree and run an INSERT statement for each item you traverse.
Re: How to save the content from a QTreeWidget (tree structure, model) to database
What is the value(index) for my INSERT statement if i iterate over the tree?
Code:
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?
Re: How to save the content from a QTreeWidget (tree structure, model) to database
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.
Re: How to save the content from a QTreeWidget (tree structure, model) to database
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?
Re: How to save the content from a QTreeWidget (tree structure, model) to database
Quote:
Originally Posted by
whitefurrows
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.
3 Attachment(s)
Re: How to save the content from a QTreeWidget (tree structure, model) to database
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.p...8&d=1305924133
but that is the result
http://www.qtcentre.org/attachment.p...7&d=1305924131
from this table
http://www.qtcentre.org/attachment.p...6&d=1305924130
The tree was build with this function
Code:
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?