Important: Please read the Qt Code of Conduct -

Getting QSqlRelationalTableModel from QSqlRelationalTableModel

  • Hi!

    I am using very good example 'sqlwidgetmapper' to get the idea how to get the relation model from QSqlRelationalTableModel. The thing is, that in my case I would need to create a chain so that I would need to create relation for the relation model, but QSqlRelationalTableModel::relationModel(int column) will return only the pointer to QSqlTableModel. This prevents the whole concept of relation for the new returned model.

    What could be the solution for it? I was checking all the examples and searching the internet but did not find reasonable solution...

  • Lifetime Qt Champion


    Can you give more details about your database schema ?

  • I will be using the following database:

    • 'producer' table with primary key (varchar, I guess this is not a problem with QSQLITE) and (among the others) key, which will relate to 'article' table
    • 'article' table with key (should I use primary key?) and the key, which will relate 'colour' table'
    • 'colour' table will have the key and the name of the colour

    I would like to be able to present nicely the combo box with the color names using the QSqlWidgetMapper depending on the current 'producer' id, which will have the assigned current 'article' id, which will in turn relate to 'colour' table with the current colour id.

  • Lifetime Qt Champion

    It's a bit unclear what you want to show on your GUI. Can you post a sketch of it ?

  • Basically it would be important for me to get all the TableModels as the QSqlRelationalTableModel. One additional reason is that I would like to subclass the QSqlRelationalTableModel and doing it separately for QSqlTableModel and QSqlRelationalTableModel would be inconvenient. So I would like to be able to get somehow QSqlRelationalTableModel from the columns of the QSqlRelationalTableModel, not only QSqlTableModel (if possible).

    What I want to achieve on the example with articles:
    in 'article' table I would have a colour id specified for each article (please have a look on the list on the tables in the post above). The color id would then relate to the specific colour from the 'colour' table, which would be shown in the table view in the corresponding column. This could be easily done with a simple relation, but if I am getting the 'article' table from 'producer' table using the QSqlRelationalTableModel::relationModel function I would get only QSqlTableModel, which would prevent me from setting any relations.

    If that still would not be clear enough, please let me know.

  • @Jendker
    I could be off-base on this, but since QSqlTableModel *QSqlRelationalTableModel::relationModel(int column) const is virtual, and QSqlRelationalTableModel is derived from QSqlTableModel, could you not override that in your own derived class and return some other existing QSqlRelationalTableModel of your own?

    You won't get a QSqlRelationalTableModel from the base QSqlRelationalTableModel::relationModel() because it only requires the FK table to be a plain QSqlTableModel.

  • @JonB Thank you! I will give it a try!

    Maybe to explain my problem in a bit different way: I hang up on the creation of the TableModel using the QSqlRelationalTableModel::relationModel(int column) because I would like to get something similar like done in this example: allow selection of the colour from the 'colour' table, which will relate to selected 'article', in relation to 'producer'. That makes article model the usual QSqlTableModel, which prevents me from using the relation. But at the same time I am not pretty sure if the convenctional creation of the separate QSqlRelationalTableModel for the 'article' table would be somehow better then creation of the whole chain of the 3 tables using QSqlRelationalTableModel::relationModel(int column)...

Log in to reply