PDA

View Full Version : Mapping two tables on the widgets using QDataWidgetMapper



stanislavf
20th September 2016, 21:19
Hello,

There are two tables in SQLite. For a simple example, person and address. How to map data of the current man from both tables to form widgets? How should we synchronize the current index of the two tables, if we use two QDataWidgetMapper's?

Is there another approach to display related data on the form, including insertion?

I would be very grateful for your help and examples.

wysota
21st September 2016, 14:41
Are you using QSqlRelationalTableModel?

stanislavf
23rd September 2016, 21:17
Yes, it is.

wysota
24th September 2016, 08:17
So why are you using two widget mappers?

stanislavf
28th September 2016, 20:55
Single mapper can map single table, doesn't it? So I had to use two mappers for both tables. The question is how to synchronize their indexes?

d_stranz
28th September 2016, 22:38
Why don't you use an SQL JOIN to make a single result table?

broddo
19th October 2016, 15:59
I pretty much have the same issue. I have two tables - a users table and an address table (which contains several necessary fields such as city, state, country etc). The tables are linked via a foreign key (address_id in the users table).

I'm trying to find a suitable way of setting up this model in QT. However, from my understanding a QSqlRelationalTableModel() won't work here as I don't simply want to replace the foreign key column in the user table with a single column in the address table - I want to replace that foreign key with ALL the columns in the address table.

Is there a suitable way of doing this or will I have to resort to QSqlQueryModel()?

anda_skoa
19th October 2016, 19:48
If you don't want to use QSqlQueryModel for whatever reason, you can easily create your own QAbstractTableModel derived model which does exactly what you need it to do.

Cheers,
_