Results 1 to 3 of 3

Thread: QSqlTableModel inserts empty rows

  1. #1
    Join Date
    May 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSqlTableModel inserts empty rows

    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):
    Qt Code:
    1. q.exec(QLatin1String("create table t1(id integer primary key, f1 varchar)"));
    2. q.exec(QLatin1String("insert into t1(f1) values('a')"));
    3. q.exec(QLatin1String("insert into t1(f1) values('a')"));
    4. q.exec(QLatin1String("insert into t1(f1) values('a')"));
    5. q.exec(QLatin1String("insert into t1(f1) values('b')"));
    6. q.exec(QLatin1String("insert into t1(f1) values('b')"));
    To copy to clipboard, switch view to plain text mode 

    Here is the sequence of signals:

    1. setfilter("f1 = 'c'") - model sholud return 0 rows
      1. rowsAboutToBeRemoved
      2. rowsRemoved
      3. modelAboutToBeReset
      4. modelReset

      Now model is empty.
    2. setFilter("f1 = 'a'") - model should return 3 rows
      1. modelAboutToBeReset (why? model was already reset at the end of previous step)
      2. rowsAboutToBeInserted
      3. rowsInserted (everything is ok, i can see 3 rows)
      4. modelReset (here is the problem)

      Now model has 3 empty rows.
    3. setFilter("f1 = 'a'") - same as setp 2
      1. rowsAboutToBeInserted
      2. 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.
    Attached Files Attached Files

  2. #2
    Join Date
    May 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel inserts empty rows

    Ok. The simplest (and probably most inefficient) solution is:
    1. disconnect model from QTableView
    2. setup filter twice
    3. connect model to QTableView


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

    Qt Code:
    1. void ModelTest::updateFilter()
    2. {
    3. bool modelIsEmpty = (model->rowCount() == 0);
    4.  
    5. if(modelIsEmpty)
    6. ui.tableView->setModel(0);
    7.  
    8. model->setFilter(QString("f1 = '%1'").arg(ui.lineEdit->text()));
    9.  
    10. if(modelIsEmpty){
    11. model->setFilter(QString("f1 = '%1'").arg(ui.lineEdit->text()));
    12. ui.tableView->setModel(model);
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

  3. #3

    Default Re: QSqlTableModel inserts empty rows

    Maybe this is the only to solve this problem,Thank you

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.