Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Update QSqlTableModel after QSqlQuery executing
Forum Updated to NodeBB v4.3 + New Features

Update QSqlTableModel after QSqlQuery executing

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsqldatabaseqsqltablemodelqsqlqueryfiltering
4 Posts 2 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EskeHagen
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      E 1 Reply Last reply
      0
      • SGaistS SGaist

        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.

        E Offline
        E Offline
        EskeHagen
        wrote on last edited by
        #3

        Is it posible you can give me an example of setFilter @SGaist ?
        That would be great!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Adapted from the documentation example: model->setFilter("should_alarm=1");

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved