QSqlQueryModel, trees and more
-
I will examplify the problem using the following scenario:
- A database that contains:
- manufacturer: id, description
- model: id, manufacturerId, description
- car: id, modelId, description
So we have a tree-like structure in the db:
@
- manufacturer
- model
- car
@
- car
- model
My target is to make every path in the tree (here the paths are guaranteed to be three) to be listed in a QComboBox.
So the final UI will be:@
"Manufacturer:" QComboBox
"Model:" QComboBox
"Car:" QComboBox
@So what I want is a tree view but displayed as a form with three rows each of which contains a combo box that contains path from the tree.
How I am currently trying to solve this:
- Subclassed QSqlQueryModel and set its query to:
@
"SELECT "
"manufacturers.id, "
"manufacturers.description, "
"models.id, "
"models.description, "
"cars.id, "
"cars.description "
"FROM "
"cars "
"INNER JOIN models "
"ON cars.modelId = models.id "
"INNER JOIN manufacturers "
"ON models.manufacturerId = manufacturers.id "
"ORDER BY "
"manufacturers.description, "
"models.description, "
"cars.description";
@
In the model I override the data() member function and the columnCount() one, so that I have three model columns:
Manufacturers
Models
Carsand for each column I have two roles:
-> Qt::DisplayRole - returns the description,
-> RoleId = Qt::UserRole + 1 - the id of the given entitySo, at this point I have a tabular model of my tree-like data.
-
Convert the QSqlQueryModel subclass to tree model:
"This thread":http://www.qtcentre.org/threads/23698-how-to-display-QSqlQueryModel-in-QTreeView recommends using QAbstractProxyModel and implement the:
-> mapToSource
-> mapFromSource
member functions. -
Make each QComboBox use the same instance of the tree model, but making it use different column from it.
@
TreeModel* const treeModel = new TreeModel( this ); // the QAbstractProxyModel subclass
manufacturerComboBox.setModel( treeModel );
manufacturerComboBox.setModelColumn( 0 );modelComboBox.setModel( treeModel );
modelComboBox.setModelColumn( 1 );carComboBox.setModel( treeModel );
carComboBox.setModelColumn( 2 );
@I am also thinking of using the QDataWidgetMapper class if using a given column for a tree-view doesn't work.
So my question is: is this the right direction e.g. create a QAbstractProxyModel, and if so how should I implement the mapToSource and mapFromSource? Do I need to implement the parent() member function?
- A database that contains: