Quantcast
Channel: Installation and Deployment
Viewing all articles
Browse latest Browse all 22112

QSqlQueryModel, trees and more

$
0
0
I will examplify the problem using the following scenario: 1) 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 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: 1) 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 Cars and for each column I have two roles: -> Qt::DisplayRole – returns the description, -> RoleId = Qt::UserRole + 1 – the id of the given entity So, at this point I have a tabular model of my tree-like data. 2) Convert the QSqlQueryModel subclass to tree model: This thread [qtcentre.org] recommends using QAbstractProxyModel and implement the: -> mapToSource -> mapFromSource member functions. 3) 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?

Viewing all articles
Browse latest Browse all 22112

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>