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. QSqlTableView does not refresh when use setQuery of QSqlQueryModel with QSqlQuery object as parameter
Forum Updated to NodeBB v4.3 + New Features

QSqlTableView does not refresh when use setQuery of QSqlQueryModel with QSqlQuery object as parameter

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 252 Views
  • 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.
  • M Offline
    M Offline
    michi
    wrote on last edited by michi
    #1

    Hello,

    I'm reworking my Qt Application (Windows Qt 5.5.1) so that it used prepared QSqlQueries instead simple query strings. The QSqlQueryModel accept both: simple query strings and QSqlQuery objects. My application needs to support MySQL and Sqlite databases so I need to transmit the database when setting a new query

    This solution works and as soon the QSqQueryModel receives a new query the connected QSqlTableView refreshed it's content.

    QSqlDatabase mySqlDatabase;
    sqltableModel.setQuery("select id, name from mytable where name = 'Peter'",  mySqlDatabase);
    

    But when using the solution using prepared Statements the QSqlTableView does not refresh the content

    QSqlDatabase mySqldatabase;
    QSqlQuery sqlQuery(mySqlDatabase);
    sqlQuery.prepare("select id, name from mytable where name = :searchstring")
    sqlQuery.bindValue(":searchstring", "Peter");
    sqlTableModel.setQuery(sqlQuery);
    

    The prepared statements are working when I execute them directly. So the SQL statement itself is o.k. but when using it in combination with QSqlQueryModel and QSqlTableView the table view does not get refreshed.

    I'm alread trying to call QSqlTableViews clear method and calling directly the QSqlTableViews query directly with

    sqlTableModel.query.exec();
    

    It was executed but without refreshing the connected QSqlTableView.

    Any idea whats going wrong here?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      michi
      wrote on last edited by michi
      #2

      Found the solution by myself. You need to exec the QSqlQuery object BEFORE set it in QSqlQueryModel object then the model is updated and signal it to the connected QSqlTableView

      QSqlDatabase mySqlDatabase;
      QSqlQuery sqlQuery(mySqlDatabase);
      sqlQuery.prepare("select id, name from mytable where name = :searchstring")
      sqlQuery.bindValue(":searchstring", "Peter");
      
      if (sqlQuery.exec())
      {
          sqlTableModel.setQuery(sqlQuery);
      }
      
      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