how to display QSqlQueryModel in QTreeView
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.
Re: how to display QSqlQueryModel in QTreeView
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.
Re: how to display QSqlQueryModel in QTreeView
result that returned by my QsSlquery is :
Code:
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:
Code:
-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.
Re: how to display QSqlQueryModel in QTreeView
You have to provide a mapping between a flat and hierarchical structure by reimplementing QAbstractProxyModel::mapToSource() and QAbstractProxyModel::mapFromSource().
Re: how to display QSqlQueryModel in QTreeView
am i supposed to loop on QSqlquery and build my own tree structure in these methods??
Re: how to display QSqlQueryModel in QTreeView
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.
Re: how to display QSqlQueryModel in QTreeView
i will try subcllassing proxymode firstl..
Thanks for your help..