0

Need some help finding why my QListView will not refresh.

I'm using QListView with a QSqlTableModel. I implement the model in the following function. I call this function from the class' constructor.

void myclass::refresh()
{
     model_path = new QSqlTableModel(this);
     model_path->setTable("mytable");
     model_path->setEditStrategy(QSqlTableModel::OnManualSubmit);
     model_path->select();
     ui->listView_path->setModel(model_path);
     ui->listView_path->setModelColumn(1);
 }

The following function will add a row and the qlistView refreshes without any issue.

 void myclass::on_pushButton_add_clicked()
 {
     QSqlRecord rec (model_path->record());
     rec.setValue(1,ui->lineEdit->text());
     rec.setValue(2,2);

     model_path->insertRecord(-1, rec);
     emit model_path->layoutChanged();
 }

The following function will remove a row based on which line is highlighted in the QListView. The remove works as the row is deleted from the database once the .submitAll is done. However the QListView does not update consistently.

 void myclass::on_pushButton_remove_clicked()
 {
     model_path->removeRow(ui->listView_path->currentIndex().row());
     emit model_path->dataChanged(ui->listView_path->currentIndex(),ui->listView_path->currentIndex());
     emit model_path->layoutChanged();
 }

If I delete a row, the list won't refresh. If I add one or more new rows, and then delete one or all of them, they will get refreshed. As you can see, I use both dataChanged and layoutChanged but they don't seem to do much here.

I don't understand why the refresh is not consistent. Can anyone help?

RAM
  • 2,257
  • 2
  • 19
  • 41
David
  • 47
  • 9

1 Answers1

0

You don't need to call both layoutChanged or dataChanged when you add or delete some rows. They are not designed for such kind of updates. Check documentation

Possible, your problem is in understanding edit strategy QSqlTableModel::OnManualSubmit. Try to change it to QSqlTableModel::OnFieldChange

Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61
  • Thanks for the answer. I'm using both `layoutChanged` and `dataChanged` only to show that neither works. Also OnManualSubmit is the right edit strategy for this as I only commit to changes at the end. – David Jun 03 '14 at 15:54