Update QSqlTableModel after QSqlQuery executing
Unsolved
General and Desktop
-
Hi!
I want to update my QSqlTableModel and tableView after I create a select distinct for filtering the database.
My database consist a column ("should_alarm") that I want to filter on and only show when the value is '1'.My code is like this:
// Connect the database void MainWindow::on_pushButton_ConnectDatabase_clicked() { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(".\\Data copy 14-12-2015 Test1\\database.db3"); if (db.open()) { qDebug() << "SQL database is connected."; } else { qDebug() << "ERROR: SQL database is NOT connected."; } model = new QSqlTableModel(this, db); model->setTable("datalog"); model->setEditStrategy(QSqlTableModel::OnManualSubmit); model->select(); ui->tableView_Database->setModel(model); } // Test button for start the filtering void MainWindow::on_pushButton_testFilter_clicked() { QSqlDatabase db = QSqlDatabase::database(); QSqlQuery query(db); query->exec("SELECT * FROM datalog WHERE should_alarm=1"); model.submitAll(); // Not sure about how to "update" the model and tableView }
The problem is that the tableView doesn't update the view after the filtering. I don't know how the update process works, so I hope some one can help me.
-
Adapted from the documentation example:
model->setFilter("should_alarm=1");