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. Assigning roles to QSqlRelationalTable in QT5
Qt 6.11 is out! See what's new in the release blog

Assigning roles to QSqlRelationalTable in QT5

Scheduled Pinned Locked Moved General and Desktop
5 Posts 1 Posters 1.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.
  • O Offline
    O Offline
    onezeno
    wrote on last edited by
    #1

    I am using "this tutorial":http://qt-project.org/wiki/QML_and_QSqlTableModel to access a SQLite table and make it available in QML. The populates a number of empty rows, but I cannot get any of the fields (or roles) to display. Any idea what I could be missing?

    My qml code is something like this, where "STRING" is a field, and a valid role that I can observe in the C++ object with the debugger:

    @
    TableView {
    model: myModel

    TableViewColumn {
    role: "STRING"
    title: "STRING"
    width: 160
    }
    }
    @

    EDIT: See my own comment trails below, but I believe the question here boils down to: How can I assign roles to QSqlRelationalTableModel when "setRoleNames()":http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel-compat.html#setRoleNames has been deprecated?

    1 Reply Last reply
    0
    • O Offline
      O Offline
      onezeno
      wrote on last edited by
      #2

      I notice that when I inspect my QLSqlTableModel with the debugger, the roles have been updated. However, when I inspect the roles using the roleNames() method, I observe the six default role names (toolTip, display, edit, etc.)

      Here's my code used to generate the role names. Notice I commented out the setRoleNames in the #ifndef QT5 directive. It was throwing a compiler error, and I was hoping that since I'm using QT5 it would be okay.

      @
      void QLSqlTableModel::generateRoleNames()
      {
      roles.clear();
      int nbCols = this->columnCount();
      for (int i = 0; i < nbCols; i++) {
      roles[Qt::UserRole + i + 1] = QVariant(this->headerData(i, Qt::Horizontal).toString()).toByteArray();

      }
      

      #ifndef HAVE_QT5
      // setRoleNames(roles);
      #endif
      }
      @

      1 Reply Last reply
      0
      • O Offline
        O Offline
        onezeno
        wrote on last edited by
        #3

        I overloaded roleNames() to return roles, so now the returned roles of that method are my 11 column names, rather than the 6 default roles. However, my data still does not show up in TableView. If I change the role name to one of the 6 default values, say "edit", then TableView is populated with a bunch of int values.

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

          Ah, I think I see the problem. Roles is defined in the derived class, not QSQLRelationalTableModel class. So when I generatRoleNames, I'm not really doing anything. How then can I assign roleNames in the base class? "setRoleNames()":http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel-compat.html#setRoleNames function has been deprecated.

          1 Reply Last reply
          0
          • O Offline
            O Offline
            onezeno
            wrote on last edited by
            #5

            I ended up just overriding the roleNames() method.

            @QHash<int, QByteArray> QLSqlTableModel::roleNames() const{
            return roles;
            }@

            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