How can I use QSqlRelationalTableModel to read-write the result set from a query?

  • I want to select some fields from a database, show them in a QTableView and then I want the posibility to edit them and write them back to the database.
    I can use model->setQuery() method only with QSqlQueryModel, but that doesn't allow writing data.
    QSqlTableModel and QSqlRelationalTableModel doesn't allow to use setQuery();
    I ended up using "create view" sql statement (which creates a virtual table) and use model->setTable() for the virtual table created, but that looks to me a clumsy way.
    What is the standard way for doing read-write with QSqlTableModel/QSqlRelationalTableModel?

  • By setting the table name (setTable), the where clause (setFilter), etc. and allowing Qt to build and perform the underlying SQL query. You cannot of course write back arbitrary SQL queries (how is a "SELECT COUNT(*) FROM FOO" supposed to be writeable?).

  • Can you give an example how to show in a QTableView all columns in a QSqlRelationalTableModel without the one with the name 'foo'?

  • octal, I mean how do you use SQL queries, and QTableView to show the result set. Can be used QTableView and QSqlRelationalTableModel methods for all the SQL queries?

  • How can be updated the QTableView (in real time) while you enter some values in a database (using QLineEdit, QComboBox, QSpinBox, .... for introducing the values)? Example.: I introduce some values and when I press QPushButton the values are added to the database and QTableView adds a row with the new record in the database.

Log in to reply