PDA

View Full Version : QSqlRelationalTableModel looks flat in QTreeView



davethomaspilot
12th December 2012, 20:01
I set up a QSqlRelationalTableModel for a QTreeView using a QSqlRelation to associate a key in two different tables.

I expected the QTreeView to show a parent/child relationship (a tree) based on the QSqlRelation, but it looks flat like the QTableView. Is this the expected behavior?

I wanted something that would expand when I clicked on the parent to show child records. For example, I have a "clubs" table with team names, and a different table "dogs" that are associated with an id. I hoped the QTreeView would show only field(s) from the club table until a row was clicked to "drill down" to display the associated records from the dogs table.

Do I need to do a custom model for this? Is there any point in doing a model/view approach instead of the simpler QTreeWidget, if I have to do a custom model anyway?

Thanks,

Dave Thomas

davethomaspilot
12th December 2012, 22:52
So, it looks like I need to do something like I see in

examples/itemviews/simpletreemodel

Is that correct, or am I missing some easier way?

Thanks,

Dave Thomas

davethomaspilot
13th December 2012, 22:55
Wow, the silence is deafening on this one. Maybe know one knows enough to reply or perhaps I've been offensive some how.

If it's the latter, it was intentional, and accept my apology.

I found class that does what I need: SqlTreeModel. I found it here:

http://www.mimec.org/node/300

Dave Thomas

http://www.mimec.org/node/300

wysota
14th December 2012, 00:00
Sorry, I missed your thread completely. That's what usually happens if one replies to his own posts :)

QSqlRelationalTableModel is a flat model, nothing will make it build a tree.

davethomaspilot
14th December 2012, 00:36
Thanks for replying.

So, while the SqlTreeModel class works, I'm still wondering if there is any advantage to doing a model/view tree instead of an item based tree for Sql query based data.

As I understand it, the advantage of the model/view approach is that multiple views can be used for the same data model. But, I don't see more than one view available for a tree. So, why bother to do model/view for a tree versus just a tree widget?

Also,in either case, the data source will actually be the database. Either I copy data from there into a tree model, or to an item based tree widget. I have to do all the work to keep those copies of the data in sync with the database.

What's the intended advantage of a QTreeModel/QTreeView versus the item based tree widget?

Thanks,

Dave Thomas

ChrisW67
14th December 2012, 00:38
Do I need to do a custom model for this? Is there any point in doing a model/view approach instead of the simpler QTreeWidget, if I have to do a custom model anyway?
Yes. There is a point to a model rather than using a QTreeWidget if you want more than one view on the data at the same time.

davethomaspilot
14th December 2012, 00:56
But, isn't there only one view widget for tree structures available in Qt? Or do you mean multiple instances of that treeview on the same data model?

Thanks,

Dave Thomas

wysota
14th December 2012, 10:53
But, isn't there only one view widget for tree structures available in Qt? Or do you mean multiple instances of that treeview on the same data model?

You can show a tree model in a QTableView or a QListView as well.

Another advantage of having proper models over convenience views such as QTreeWidget is less data redundancy. If you have some data and you want to put it into QTreeWidget, you have to effectively make a copy of the data (unless you want to reimplement have a dozen different things). With a model approach you only need to access your original data.