PDA

View Full Version : QSqlTableModel::removeRows returns false



caster89
26th October 2014, 09:33
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);


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";
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;


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

caster89
29th October 2014, 19:42
Hi everyone,
I realise the post is a bit vague but I do not know how to explain myself more clearly. Is there anything I can add to make the situation clearer and maybe attract more solutions? :)
Thank you very much

ChrisW67
29th October 2014, 20:35
What is the value of "row" before your original removeRows() call. The table view can have no current index.

caster89
30th October 2014, 09:29
I tried both using the current index (which always returned a valid number) and I also tried calling removeRows() directly with a number. Also I added a


qDebug()<<"Invalid Row"

within the initial if check but the message is never displayed.

caster89
31st October 2014, 14:25
Ok if anyone happens to have the same problem (I highly doubt it :) ) I created a new project and copied the code line by line to see where the problem was. Apparently I had forgotten to add QSqlTableModel::submit() at the end of my custom submit function, and that was the problem. For some reason the other functions (Insert update etc.) were not affected by this problem.