2

I'm working on a project that contains a QTreeWidget. This widget will have Parent nodes with childs nodes and or other siblings nodes. Attached to each node will be a unique ID in the form of an integer as well as a name.

Now..... what methods can or should I use to save all the nodes in the QTreeWidget to disk and how to read them back in?????

What I would do is traverse the tree and save each node into a sqlite DB. I already can do that in exactly the same order as it is in the tree. BUT...... QTreeWidget and its related QTreeWidgetItem objects do NOT have a uniq index that you can USE to insert other nodes around.

When I rebuild the tree, childs have to be attached to their parents/ancestor (which by then exists in the tree). To do so I keep track of the Parents and their IDs I already added to the tree, like (parent name, parentID) inside an array. Note that the parentID has nothing to do with location as a node inside the QTreeWidget/WidgetItem. It is created by myself and stored insite the sqlite db allong with the name of the node just as some sort of tag since QTreeWidget/QTreeWidgetItem does not supply any.

To determine the correct parent I

  1. take the ParentID from the sqlite DB,
  2. search the array or the QTreeWidget for that same ID,
  3. select that parent in the QTreeWidget,
  4. add the sqlite db item as a child node to the parent in the treeview.

But it feels clumsy this way, because of the search each time a node is added and because of a missing index inside the QTreeWidget/WidgetItem I can use for inserting nodes around.

I know this should be done much more elegant and less resource intensive. I'm not looking for exact coding examples but more for ideas and hints of the methods and properties to use from QTreeWidget and QTreeWidgetItem.

Any ideas?

César
  • 9,939
  • 6
  • 53
  • 74
thedax
  • 131
  • 1
  • 5

2 Answers2

0

I managed this by using the data property of the TreewidgetItem by setting a value in it. Using that value as a reference to find one or more or even all TreewidgetItem(s) makes it easy to save a complete tree.

thedax
  • 131
  • 1
  • 5
-1

Wouldn't it be more natural to serialize your treewidget as xml?

If so, the QXmlStream Bookmarks Example might provide some inspiration.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336