[SOLVED] QSqlTableModel::removeRow problem.
-
Hi, everyone again!
I have no idea why, but when i am trying to use QSqlTableModel::removeRow(int numb) method, i've got deleted all records with same fields instead of deleting only 1 of them.
I am using QSQLITE Database.
All records adds via "INSERT INTO ..." query.Here are 3 screenshots of this process:
1)
!http://sphotos-a.ak.fbcdn.net/hphotos-ak-snc6/254358_2949167585391_1829528596_n.jpg(1)!!http://sphotos-g.ak.fbcdn.net/hphotos-ak-snc6/254358_2949167625392_663510958_n.jpg(2)!
!http://sphotos-f.ak.fbcdn.net/hphotos-ak-ash4/254358_2949167665393_807131696_n.jpg(3)!
@bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
if((event->type() == QEvent::MouseButtonPress) && (dynamic_cast<QMouseEvent>(event)->button() == Qt::LeftButton))
{
model->removeRow(index.row());
qDebug()<<"Row removed. "<<model->rowCount()<<"row(s) stayed.";
return true;
}
else
return QItemDelegate::editorEvent(event, model, option, index);
}@I have traced via qDebug(), that the removeRow() method has called only once... But it is still few records deleted, if theirs fields are same.
What i am doing wrong? -
[quote author="qxoz" date="1363926934"]Hi!
Can you show your CREATE TABLE script for "Текущие заявки"?[/quote]Hi!
I have solved the problem. it was in lack of PRIMARY KEY in my table. So, the deleteRow() function seems to delete rows (even if there are a few) in database, that have the same fields as a row in QSqlTableModel. I guess removeRow() function it's a query like "DELETE * FROM tableName WHERE modelField1 = DatabaseField1, modelField2 = DatabaseField2 etc..."and here is the code, that you were asking about:
@bool DataBaseZayavka::createTablePotok()
{
QSqlQuery query;
qDebug()<<"Creating table...";QString str = "CREATE TABLE potok ( " "NF INTEGER, " //Номер формы PRIMARY KEY "VP INTEGER, " //Вид перевозки PRIMARY KEY "KP INTEGER(3), " //Код получателя PRIMARY KEY //here are a lot of fields... "Z4 VARCHAR(28) " //пункт следования по признаку далее ");"; if(!query.exec(str)) { qDebug()<<"Error of creating table potok: " + query.lastError().text(); return false; } return true;
}
@ -
Good. How did you solve it?
And about table, i think be better always create the ID column even if you don't use it right now:
@QString str = "CREATE TABLE potok ( "
"id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
"NF INTEGER, " //Номер формы PRIMARY KEY
"VP INTEGER, " //Вид перевозки PRIMARY KEY
"KP INTEGER(3), " //Код получателя PRIMARY KEY
//here are a lot of fields...
"Z4 VARCHAR(28) " //пункт следования по признаку далее
");";
@ -
[quote author="qxoz" date="1364189421"]Good. How did you solve it?
And about table, i think be better always create the ID column even if you don't use it right now:
@[/quote]Thank you for advice!
Hm.. seems like "solve" isn't a correct word for such situation. I am just guess, that it is right.bool QSqlTableModel::selectRow(int row) [virtual slot]
Refreshes row in the model with values from the database table row matching on primary key values. Without a primary key, all column values must match.In this function you can see the same functionality, i guess.
It's just "SELECT" operator somewhere deeper instead of "DELETE" like removerRow() has.
But it's only a guess, so i don't pretend it is truth in final instance =) -
Yeah, it's seems like problem has solved by adding a new PRIMARY KEY column. I hide it and table works fine: 2 similar (except hiding column ID) rows are deleting separately.
-
lol :)