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. Can't insertColumn to an QSqlTableModel
Forum Updated to NodeBB v4.3 + New Features

Can't insertColumn to an QSqlTableModel

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 7.5k 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.
  • R Offline
    R Offline
    ReHa1
    wrote on last edited by
    #1

    Hi,

    I have a SQlTableModel on a database that returns 4 columns displayed in a QTableView.
    This works fine so long.

    But I wan't to add an extra column to my model, to display some database-independent data on it.
    But that doesn't work. I only see my 4 columns got from database. Any ideas, whats wrong?
    Thanks for help:

    @ _model = new QSqlTableModel();
    _model->setTable("machines");
    _model->select(); // Get column 0..3 out from database correctly

    _model->insertColumn(4); // Without any effect
    _model->setHeaderData(4, Qt::Horizontal, "MyExtraData"); // Without any effect

    _tableView = new QTableView();
    _tableView->setModel(_model);
    @

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

      Obviously you can't add columns to a model which is showing SQL data. Use a proxy model instead.

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

      1 Reply Last reply
      0
      • R Offline
        R Offline
        ReHa1
        wrote on last edited by
        #3

        I can't believe.
        Then the method QSqlTableModel::insertColumn() would be quite useless! ??

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Of course. The insertColumn is inherited from QAbstractItemModel, and is needed for other uses of the item view system. But Qt's support does not extend to modifying the database structure, and that also extends to the SQL based model-view classes. If you want to do that, you will need to implement that yourself.

          Edit:
          What do you want to achieve exactly? Do you really want to add columns to your actual table in your database, or do you just want to show additional columns with data coming from somewhere else?

          1 Reply Last reply
          0
          • R Offline
            R Offline
            ReHa1
            wrote on last edited by
            #5

            I only want to display an image (located on file-system) in the extra-column, depending on the values out of the database (columns before).
            (I don't want to add a column to the database itself).

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              OK, in that case, you have to go with a proxy model. There are several examples of using a proxy model here on the forum to add or modify data.

              Start out with subclassing QSortFilterProxyModel (Qt 4.7) or QIdentityProxyModel (Qt 4.8) as your starting point. Then, reimplement the columnCount() method to return the columnCount of your base model + 1. Then, you'll need to handle the calls to data(), headerData() and flags() for your new column.

              1 Reply Last reply
              1

              • Login

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