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?