PDA

View Full Version : QSqlRelationalTableModel doesn't edit all rows after filtering



slobo_n
24th March 2009, 13:08
Hi all.
The problem is at the bottom.
First i create my tables:


query.exec("CREATE TABLE grupi("
"grupa_id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,"
"grupa VARCHAR(10) NULL UNIQUE);");
query.exec("CREATE TABLE contacts("
"contact_id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,"
"FirstName VARCHAR(30) NULL,"
"LastName VARCHAR(30) NULL,"
"grupaId INT NOT NULL,"
" FOREIGN KEY (grupaId) REFERENCES grupi);");
query.exec("insert into grupi values('VIP')");
query.exec("insert into grupi values('Family')");
query.exec("insert into grupi values('Ungrouped')");
query.exec("insert into grupi values('Work')");

The model gets populated, and i can see all of the contacts in the database:

dataManagementModel = new QSqlRelationalTableModel(this);
dataManagementModel->setTable("contacts");
dataManagementModel->setRelation(3, QSqlRelation("grupi", "grupa_id", "grupa"));
dataManagementModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
bool ok1 = dataManagementModel->select();

The problem:
After i have created the tables and populate them with some data, the problem is when i want to delete a group. If i want to delete a group, first the contacts that were in that group must be set to another group. Let say to 'Ungrouped'. The code for this action is:

void DataManagement::setContactsToDefaultGroup(QModelIn dex oldData, QModelIndex newData)
{
dataManagementModel->setFilter("grupaId = " + oldData.data().toString()); //to get only the contacts that are in the group that's going to be deleted
bool ok = dataManagementModel->select();
int rowsCounted = dataManagementModel->rowCount();
for (int row = 0; row < rowsCounted; row++)
{
QModelIndex indexForUpdate;
QSqlRecord rec = dataManagementModel->record(row);
QString name = rec.value(1).toString(); //after some time it doesn't read the name
indexForUpdate = dataManagementModel->index(row, 3, QModelIndex()); //gives invalid index after some time
dataManagementModel->setData(indexForUpdate, newData.data().toInt());
QSqlError h = dataManagementModel->query().lastError();
bool b = dataManagementModel->submitAll();
Q_ASSERT(b);
}
dataManagementModel->setFilter(""); //to show all the contacts again
dataManagementModel->select();
}
This function can read and edit the first 2-3 contacts and fails. Is dataManagementModel used properly? How to move contacts from one group to another group?


As for convenience I have provided part of the code that you can build.

janus
24th March 2009, 22:06
Hi,

why are you not using a simple query to first update the group field (e.g. set it to "ungrouped), delete the group with another query and then call select on the model?

slobo_n
24th March 2009, 23:17
Thanks for the quick reply.

Hi,

why are you not using a simple query to first update the group field (e.g. set it to "ungrouped), delete the group with another query and then call select on the model?

I have tried and worked :). Thanks a lot.