Qt World Summit: Register Today!


QSqlRelationalTableModel - relation with multiple columns



  • Hi everybody.
    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.
    Problem:
    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?



  • 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.



    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...)?

    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 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.



  • I have the same problem, can anyone to solve this?
    for example:
    table1:

    id  shotname longname
    

    table2:

    id list table1Id 
    

    in the QSqlRelationalTableModel example , we can have these fields result:

    id list shotname
    
    // or this
    id list longname
    

    but how can we display more column like this?:

    id list shotname longname
    

    here is my test and work, but CANNOT be editable:

        QSqlQuery query(db);
        QSqlQueryModel *m = new QSqlQueryModel(this);
        query.exec("SELECT table2.id, table2.list, table1.shotname, table1.longname FROM table2"
                   "INNER JOIN table1 ON table2.table1Id= table1.id");
        m->setQuery(query);
    
        ui->tableView->setModel(m);
    


  • @QtTester
    The title of this thread was

    QSqlRelationalTableModel - relation with multiple columns

    Your solution, and the ones mentioned above, do not relate to a QSqlRelationalTableModel. That can have multiple QSqlRelations if desired. I do not know whether that allows the same column to be mapped twice to two different columns in one foreign table, but you could try it, I can't see why it would not be allowed. Ah, no, I see, it can have multiple setRelation(column, QSqlRelation())s, but each column specified can only have one relation, not multiple ones in a list, it seems.

    Your SELECT statement is fine, but yes of course what you get from that is a non-editable model.


Log in to reply