PDA

View Full Version : QSqlTableModel inserts empty rows



Nesbitt
22nd May 2008, 12:08
Hi

I have noticed that QSqlTableModel inserts empty rows after setting setFilter when previous setFilter returned 0 rows. I am using Qt 4.3.4.

Similar problems ware described in:
Bug 170783
Bug 180617
Bug 194644
Bug 203724

I have check the sequence of signals emitted by QSqlTableModel (QAbstractItemModel). My example table (database is SQLite):

QSqlQuery q;
q.exec(QLatin1String("create table t1(id integer primary key, f1 varchar)"));
q.exec(QLatin1String("insert into t1(f1) values('a')"));
q.exec(QLatin1String("insert into t1(f1) values('a')"));
q.exec(QLatin1String("insert into t1(f1) values('a')"));
q.exec(QLatin1String("insert into t1(f1) values('b')"));
q.exec(QLatin1String("insert into t1(f1) values('b')"));


Here is the sequence of signals:


setfilter("f1 = 'c'") - model sholud return 0 rows

rowsAboutToBeRemoved
rowsRemoved
modelAboutToBeReset
modelReset

Now model is empty.
setFilter("f1 = 'a'") - model should return 3 rows

modelAboutToBeReset (why? model was already reset at the end of previous step)
rowsAboutToBeInserted
rowsInserted (everything is ok, i can see 3 rows)
modelReset (here is the problem)

Now model has 3 empty rows.
setFilter("f1 = 'a'") - same as setp 2

rowsAboutToBeInserted
rowsInserted

Now model has 6 rows (3 empty and 3 valid).


It looks like resetting model in step 2 is not necessary. Is there any way to get rid of it? Or to fix the order of signals? modelReset sholud be right after modelAboutToBeReset not after rowsInserted.

Unfortunately QAbstractItemModel::reset() is not virtual so I cannot reimplement it.

Thanks.

PS Example program is in the attachment.

Nesbitt
24th May 2008, 15:20
Ok. The simplest (and probably most inefficient) solution is:

disconnect model from QTableView
setup filter twice
connect model to QTableView


So updateFilter() in my example now looks like this:


void ModelTest::updateFilter()
{
bool modelIsEmpty = (model->rowCount() == 0);

if(modelIsEmpty)
ui.tableView->setModel(0);

model->setFilter(QString("f1 = '%1'").arg(ui.lineEdit->text()));

if(modelIsEmpty){
model->setFilter(QString("f1 = '%1'").arg(ui.lineEdit->text()));
ui.tableView->setModel(model);
}
}

notalklu
6th August 2008, 12:47
Maybe this is the only to solve this problem,Thank you