qtqsqlrelationaltablemodel

QSqlRelationalModel: After I call setRelation(...), the column in the model seem to be replaced by the column in the foreign table


After setRelation(...), How can I get the original value of the column which seems have already been replaced by the column in the foreign table?

Here's my code:

Table "record" contains "recordId integer, bookId integer, bookName varchar, author varchar, genre integer, userId integer, userName varchar, borrowDate date, returnDate date, recordState integer".

Table "genres" contains "id integer primary key, genre varchar".

    QSqlRelationalTableModel *model = new QSqlRelationalTableModel;

    model->setTable("record");

    model->setRelation(4, QSqlRelation("genres", "id", "genre"));

    QSqlRecord bookRecord = model->record(0);

    QVariant value = bookRecord->value(4);

The line QVariant value = bookRecord->value(4); returns contents in table "genres" which is QString instead of the original value genre in table "record" which is integer

Here I want to assign the original value of bookRecord at section 4 to the QVariant variable value. How can I do it?

Can anyone give me some suggestions?


Solution

  • Eventually, I used a not much elegant method to solve the problem.

    I create a new QSqlquery object and query the original table directly.

    It's not very smart, but anyway, the problem is solved.