Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlRelationalTableModel woes when selecting from a view
Qt 6.12 LTS is out! See what's new in the release blog

QSqlRelationalTableModel woes when selecting from a view

Scheduled Pinned Locked Moved General and Desktop
3 Posts 1 Posters 3.0k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hey,
    I'm having trouble with QSqlRelationalTableModel.
    In my database, I've defined a view with columns from a couple of tables. When trying to get my data, I run into some erratic behavior: for some records (consistently for the same ones, but without any rhyme or reason that I can see), retrieving the values of some roles fails inexplicably.

    I have an overloaded @QVariant QSqlRelationalTableModel::data(const QModelIndex& index, int role) const@ function, and I've tried to figure out what's wrong by doing a
    @qDebug() << query().lastQuery() << " failed to return a valid " << m_roles[role];@
    whenever the value returned by @value = model.record(index.row()).value(QString(m_roles[role]));@ is not valid. The documetation for @QVariant QSqlRecord::value ( const QString & name ) const@ says that "If field name does not exist an invalid variant is returned", and does not mention any other conditions that might lead to an invalid value being returned. However, the behavior I'm observing is that for some rows in the view, everything is fine; for others, I get invalid values for some or all roles. When I run the query returned by lastQuery manually, I get a valid record with valid values in all fields. There's no discernible difference between rows that work, and rows that do not.

    What's wrong? Is there something I could try, or is this a bug?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Just a quick update: it seems like even subsequent calls to the data function about the same row might return valid/invalid values (in other words, even when the query shown by lastQuery remains the same, a given role may or may not be found in the record).

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Never mind - the problem was elsewhere in the program.

        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
        • Unsolved