Important: Please read the Qt Code of Conduct -

QSqlRelationalTableModel woes when selecting from a view

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

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

  • Never mind - the problem was elsewhere in the program.

Log in to reply