2

QSqlRelationalModel を使用していますが、問題があります。

たとえば、次のような単純なテーブルを扱う場合:

Location
+------+--------+
|  id  |  name  |
+------+--------+

Department
+------+-------------+
|  id  | location_id |
+------+-------------+

次に、次のように記述できます。

departmentModel = new QSqlRelationalTableModel(this);
departmentModel->setTable("Department");
departmentModel->setRelation(Department_LocationId, QSqlRelation("Location", "id", "name"));
departmentView = new QTableView;
departmentView->setModel(departmentModel);
departmentView->setItemDelegate(new QSqlRelationalDelegate(this));

これで問題なく動作し、ID の代わりに場所の名前が表示されます。

しかし、私の場合、このアプローチを適用することはできません。次のテーブルがあるとします。

Person
+------+-------------+
|  id  |  firstname  |
+------+-------------+

Experience
+------+------------------+
|  id  |  person_id (FK)  |
+------+------------------+

Participant
+------+-----------------+
|  id  |  experience_id  |
+------+-----------------+ 

Participant を QSqlRelationalTable として使用するとします。

QSqlRelationalTable participantModel;
participantModel->setTable(Participant);
...
participantView->setModel(participantModel);
participantView->setItemDelegate(new QSqlRelationalDelegate(this));

そして、ビューに experience_id の代わりに Person.firstname を表示したい (また、編集機能を失いたくない)。これどうやってするの?

上記の例のように setRelation() を使用することはできません。

participantModel->setRelation("experience_id", QSqlRelation("Experience", "id", WHAT_DO_I_HAVE_TO_WRITE_HERE_TO_GET_WHAT_I_WANT);

person_id の代わりに firstname を表示したいので、「person_id」と書くことができません。

4

1 に答える 1