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')");
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')");
To copy to clipboard, switch view to plain text mode
The model gets populated, and i can see all of the contacts in the database:
dataManagementModel->setTable("contacts");
dataManagementModel
->setRelation
(3,
QSqlRelation("grupi",
"grupa_id",
"grupa"));
bool ok1 = dataManagementModel->select();
dataManagementModel = new QSqlRelationalTableModel(this);
dataManagementModel->setTable("contacts");
dataManagementModel->setRelation(3, QSqlRelation("grupi", "grupa_id", "grupa"));
dataManagementModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
bool ok1 = dataManagementModel->select();
To copy to clipboard, switch view to plain text mode
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:
{
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++)
{
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();
}
void DataManagement::setContactsToDefaultGroup(QModelIndex 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();
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks