Hi everyone,
I am currently trying to make simple program to connect to a sql database, since I want to have several tables with relationships I decided to try and subclass QSqlTableModel, but I encountered some problems. What I want to do is use the original removeRows to clear the Main table I work on and then add a little bit of code to remove the related data from the other tables; but for some reason when I try to do removeRows I always get false. I also tried calling directly the original method without subclassing it but I still get false.
Here is the code I use to call removeRows:
int row = dbTableView->currentIndex().row();
dbmodel->removeRows(row,1);
int row = dbTableView->currentIndex().row();
dbmodel->removeRows(row,1);
To copy to clipboard, switch view to plain text mode
On the other hand if I subclass removeRows and basically copy the method from the original code I am able to delete the record without problems. So basically if I use this code:
bool MySqlTableModel
::removeRows(int row,
int count,
const QModelIndex &parent
){
if (parent.isValid() || row < 0 || count <= 0)
return false;
int i;
switch (editStrategy()) {
case OnFieldChange:
case OnRowChange:
for (i = 0; i < count; ++i) {
if (row + i == insertIndex)
qDebug()<<"Should revert row";
return false;
}
select();
break;
case OnManualSubmit:
for (i = 0; i < count; ++i) {
int idx = row + i;
if (idx >= rowCount())
return false;
}
break;
}
return true;
bool MySqlTableModel::removeRows(int row,int count, const QModelIndex &parent){
if (parent.isValid() || row < 0 || count <= 0)
return false;
int i;
switch (editStrategy()) {
case OnFieldChange:
case OnRowChange:
for (i = 0; i < count; ++i) {
if (row + i == insertIndex)
qDebug()<<"Should revert row";
else if (!QSqlTableModel::deleteRowFromTable(row + i))
return false;
}
select();
break;
case OnManualSubmit:
for (i = 0; i < count; ++i) {
int idx = row + i;
if (idx >= rowCount())
return false;
}
break;
}
return true;
To copy to clipboard, switch view to plain text mode
Also if I just call deleteRowFromTable I am successful and it returns true.
What could be returning me false when I call the original method through QSqlTableModel::removeRows()?
I am using Qt 5.2.1 on a Mac OSX Mavericks.
Thank you very much
Bookmarks