Hi,
This is my first post, because although you learn a lot from visits this forum, still have not found answer to this problem (apparently simple).
I have a QSqlRelationalTableModel and want to access the index value of the relationship without making a search related model.
For example:
Tables
mysql> describe user;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| IdCategory | int(5) | NO | MUL | NULL | |
| UserName | varchar(40) | NO | PRI | NULL | |
| IdPerson | int(11) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
mysql> describe category;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| IdCategory | int(5) | NO | PRI | NULL | auto_increment |
| NameCategory | varchar(80) | NO | UNI | NULL | |
+--------------+-------------+------+-----+---------+----------------+
mysql> describe user;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| IdCategory | int(5) | NO | MUL | NULL | |
| UserName | varchar(40) | NO | PRI | NULL | |
| IdPerson | int(11) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
mysql> describe category;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| IdCategory | int(5) | NO | PRI | NULL | auto_increment |
| NameCategory | varchar(80) | NO | UNI | NULL | |
+--------------+-------------+------+-----+---------+----------------+
To copy to clipboard, switch view to plain text mode
Code
Connection.setHostName("192.168.0.21");
Connection.setDatabaseName("brb");
Connection.setUserName("user");
Connection.setPassword("password");
Connection.open();
// table user
modelUser->setTable("user");
modelUser->sort(modelUser->fieldIndex("UserName"), Qt::AscendingOrder);
modelUser
->setRelation
(modelUser
->fieldIndex
("IdCategory"),
QSqlRelation("category",
"IdCategory",
"NameCategory"));
modelUser->select();
// table category
Category->setTable("category");
Category->select();
int i, j;
for(i = 0; i < 3;
for(j = 0; j < modelUser->record().count(); j++)
qDebug()<<modelUser->record(i).fieldName(j)<<" :"<<modelUser->record(i).value(j).toString();
qDebug()<<modelUser->record(0).value("IdCategory").toString();
qDebug()<<modelUser->record(1).value("IdCategory").toString();
qDebug()<<modelUser->record(2).value("IdCategory").toString();
QSqlDatabase Connection;
Connection = QSqlDatabase::addDatabase("QMYSQL");
Connection.setHostName("192.168.0.21");
Connection.setDatabaseName("brb");
Connection.setUserName("user");
Connection.setPassword("password");
Connection.open();
// table user
QSqlRelationalTableModel* modelUser;
modelUser = new QSqlRelationalTableModel;
modelUser->setTable("user");
modelUser->sort(modelUser->fieldIndex("UserName"), Qt::AscendingOrder);
modelUser->setRelation(modelUser->fieldIndex("IdCategory"), QSqlRelation("category", "IdCategory", "NameCategory"));
modelUser->select();
// table category
QSqlTableModel* Category;
Category = new QSqlTableModel;
Category->setTable("category");
Category->select();
int i, j;
for(i = 0; i < 3;
for(j = 0; j < modelUser->record().count(); j++)
qDebug()<<modelUser->record(i).fieldName(j)<<" :"<<modelUser->record(i).value(j).toString();
qDebug()<<modelUser->record(0).value("IdCategory").toString();
qDebug()<<modelUser->record(1).value("IdCategory").toString();
qDebug()<<modelUser->record(2).value("IdCategory").toString();
To copy to clipboard, switch view to plain text mode
Application output:
Starting C:\Qt\2010.04\projects\teste-build-desktop\debug\teste.exe...
"NameCategory" : "ADMINISTRATOR"
"UserName" : "beatriz"
"IdPerson" : "0"
"NameCategory" : "MANAGER"
"UserName" : "ana"
"IdPerson" : "0"
"NameCategory" : "SUPERVISOR"
"UserName" : "bob"
"IdPerson" : "0"
""
""
""
Starting C:\Qt\2010.04\projects\teste-build-desktop\debug\teste.exe...
"NameCategory" : "ADMINISTRATOR"
"UserName" : "beatriz"
"IdPerson" : "0"
"NameCategory" : "MANAGER"
"UserName" : "ana"
"IdPerson" : "0"
"NameCategory" : "SUPERVISOR"
"UserName" : "bob"
"IdPerson" : "0"
""
""
""
To copy to clipboard, switch view to plain text mode
My question is whether it is possible to access the field value user.IdCategory without having to create a search algorithm in the model?
Can someone give me a direction?
The solution to this doubt, not only serves me in this simple example, but it helps me a broader understanding on the very QSqlRelationalTableModel.
My previous experience was with CBuilder / Zeoslib / MySql.
Thank you in advance and excuse my English.
Bookmarks