Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QSqlRelationalTableModel - relation with multiple columns
I know there is already a post related to this topic ("this one":http://qt-project.org/forums/viewthread/7744), but I don't seem to get this work.
I have one database table containing a list of people, and another table that has a foreign key to the id of the person. If I wanted to display first name and last name of a person in a table view or while using a mapper, given that first name and last name are in different columns, how could I do this using QSqlRelationalTableModel?
Link suggested to subclass QAbstractProxyModel, I tried to subclass QSortFilterProxyModel and reimplement the data() function setting the "person" model as source model, but in this way mapper does not work: data are displayed inside the combobox (initially empty), but navigating through the records the value inside the combobox does not change...
Moreover, I don't understand what should be the source model of my proxy model: should it be the relational model, or the table model "pointed to" by the relation?
Some hints on how to proceed would be greatly appreciated :)
Does anybody know the solution?
franku last edited by
Keep trying with the QSqlRelationalTableModel. Have you read the "documentation":http://doc.qt.digia.com/qt/qsqlrelationaltablemodel.html? There you can find an example on how to make multiple relations.
Yes, I read the code snippet related to the creation of multiple relationships, but that's not what I'm trying to achieve.
I would like to have something like this:
Table 1: employee
ID_E | First_name | Last_name | Other
Table 2: project
ID_P | Employee
Setting a QSqlRelationalTableModel on the table "project", and a relationship between the column "Employee" and the ID in the table employee, I would like to have displayed in that column both the first and last names. I see that QSqlRelationalTableModel only allows to set one column for display purpose for each relationship. I would like to display the values of two columns without having to insert an additional column in the database and setting another relationship.
I hope my problem is clearer in this way.
Thank you for your reply.
franku last edited by
I am no sql expert, but can't you use a SQLView in the database itself for this request (instead of SELECT * from project WHERE...)?
On the other hand you could use just the QSqlTableModel and reimplement the ::data() and ::columnCount() to fake the extra column for the Project's QTableView.
[quote author="franku" date="1351891016"]1. I am no sql expert, but can't you use a SQLView in the database itself for this request (instead of SELECT * from project WHERE...)?[/quote]
Do you mean creating a view which concatenates the first and last name and use that as "table" to QSqlTableModel?
[quote author="franku" date="1351891016"]
2. On the other hand you could use just the QSqlTableModel and reimplement the ::data() and ::columnCount() to fake the extra column for the Project's QTableView.[/quote]
That would be an option, I thought about reimplementing those function in QSqlRelationalTableModel to include also the "last_name" column when displaying data for the foreign key reference, but I would prefer a cleaner approach using a proxy model, if there exists one.
In the link I posted in the first post (which is more or less the same problem I have), there seems to be a solution using a subclass of QAbstractProxyModel, the only thing is that I can't figure it out.