Update QSqlTableModel after QSqlQuery executing
-
wrote on 21 Dec 2015, 10:47 last edited by
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.
-
Hi and welcome to devnet,
You are currently just running that query, it doesn't modify anything in your model. You should use the setFilter function for QSqlTableModel, to do the filtering you want.
-
Hi and welcome to devnet,
You are currently just running that query, it doesn't modify anything in your model. You should use the setFilter function for QSqlTableModel, to do the filtering you want.
-
Adapted from the documentation example:
model->setFilter("should_alarm=1");
1/4