PDA

View Full Version : QTreeView linked to QSqlTableModel



certqt
27th October 2010, 17:55
I am trying to populate (and later hopefully edit) a QTreeView whose nodes are stored in a SQL table.

The structure is very simple and similar to a document:


Root
|___Text
|___Text
|___Text

So the table is just:

ID: Int
Text: Varchar
ParentID: Int

I have read very many posts with similar questions and understand that the key is to use a QAbstractProxyModel between the QTreeView and the QSqlTableModel, in particular to look at implementing mapFromSource and mapToSource. I have also looked at the Interview demo which I think should be a good reference to link a flat structure to a tree.

Unfortunately being very new to Qt and still learning some of the advanced aspects of C++, I am struggling to fully understand what to do.

I would therefore really appreciate if someone has got some working code that I can try out and learn from - thanks :)

wysota
27th October 2010, 19:02
I have a working code (somewhere) but unfortunately I can't share it (I don't know where it is anyway). You have to come up with a mapping algorithm from a flat to hierarchical model (and back) and implement it in mapToSource() and mapToSource(). But if you are a beginner, I would suggest to go with a simpler solution - copy data from your flat model to a hierarchical model (like QStandardItemModel) and then after editing it, copy it back to the database.

certqt
27th October 2010, 21:26
Thank you for the idea.

I would still like to pursue the possibility of a direct link between the SQL model and the tree view, however in the meantime if could you offer a short example of how to use the QStandardItemModel that would be very much appreciated!

wysota
27th October 2010, 21:28
There are lots of examples of using QStandardItemModel in Qt documentation. Just follow the link in this post.