PDA

View Full Version : how to display QSqlQueryModel in QTreeView



mandal
1st September 2009, 10:33
Result of my sql query is hold in QSqlQueryModel; and i simply call
mTreeView->setModel(qsqlquerymodel) to view results in treeview.
TreeView displays my table; but it is not structured (there is no node or hiyerarchical structure in view.)
I have looked around forum and understood that I have to subclass QAbstractProxyModel, and override its pure virtual methods. But i have no clue what to write in its methods.

I will be grateful for any clue about how to write a proxy to have a structured display in QTreeView using QSqlQueryModel.

Thanx in advance.

wysota
1st September 2009, 13:27
We have no clue what to write in its methods as well. QSqlQuery returns a table, not a tree so don't make us guess how to convert any table into any tree. In other words: provide some details.

mandal
1st September 2009, 16:24
result that returned by my QsSlquery is :


teamId studentId lectureId
--------- ----------- ----------
team1 student1 lecture1
team1 student1 lecture3
team1 student2 lecture1
team2 student1 lecture3
team2 student1 lecture4


i assign my QSqlQuery to QSqlQueryModel using setQuery,
and then assign QSqlQueryModel to QTreeView using setModel.
As a result tree view displays my table without hiyerarchical structure just like the table above.

But I am trying to get a display like this:


-team1
-student1
-lecture1
-lecture3
-student2
-lecture1
-team2
-student1
-lecture3
-lecture4

As i learned i have to use QAbstractProxyModel; but i do not know how to do it.

wysota
1st September 2009, 16:40
You have to provide a mapping between a flat and hierarchical structure by reimplementing QAbstractProxyModel::mapToSource() and QAbstractProxyModel::mapFromSource().

mandal
2nd September 2009, 07:21
am i supposed to loop on QSqlquery and build my own tree structure in these methods??

wysota
2nd September 2009, 08:18
No. Based on a given source model index you have to be able to calculate the hierarchy (parent) and position (row, column) in the destination model.

If this all sounds too complicated for you, you can forget about query and proxy models and based on QSqlQuery and QStandardItemModel build a new model and copy the data to it from the query.

mandal
2nd September 2009, 08:25
i will try subcllassing proxymode firstl..
Thanks for your help..