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. How can I use QSqlRelationalTableModel to read-write the result set from a query?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.8k 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
    Edico
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      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?).

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Edico
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • O Offline
          O Offline
          octal
          wrote on last edited by
          #4

          See "QTableView::setColumnHidden":http://doc.qt.nokia.com/stable/qtableview.html#setColumnHidden or "QTableView::hideColumn":http://doc.qt.nokia.com/stable/qtableview.html#hideColumn

          and "QSqlTableModel::fieldIndex":http://doc.qt.nokia.com/stable/qsqltablemodel.html#fieldIndex

          1 Reply Last reply
          0
          • E Offline
            E Offline
            Edico
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • E Offline
              E Offline
              Edico
              wrote on last edited by
              #6

              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.

              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