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. SetData() in QSqlTableModel with role other than Qt::EditRole does not work
Forum Updated to NodeBB v4.3 + New Features

SetData() in QSqlTableModel with role other than Qt::EditRole does not work

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

    Hi,

    I have been trying to change the background colour of rows matching certain criteria on a QTableView that uses a QSqlTableModel as the model. For this, I modify the Qt::BackgroundRole attribute of the relevant items, using the following sort of code (ObsEditTableModel inherits from QSqlTableModel), and basically has just this one method:

    @
    void ObsEditTableModel::setObsGood(const QModelIndex & item, bool goodObs)
    {
    int i = item.row();
    for (int j = 0; j < columnCount(); j++) {
    QModelIndex cell = index(i, j);
    if (goodObs) {
    bool r = setData(cell, QVariant(QBrush(Qt::gray)), Qt::BackgroundRole);
    qDebug() << goodObs << r << i << j;
    } else {
    bool r = setData(cell, QVariant(), Qt::BackgroundRole);
    qDebug() << goodObs << r << i << j;
    }
    }
    QModelIndex tl = index(i, 0);
    QModelIndex br = index(i, columnCount()-1);
    emit dataChanged(tl, br);
    }
    @

    The problem is that setData() always returns false, and nothing changes in the view. After a few hours of Googling, I have found the following recent post with appears to describe, as far as I can tell, my exact problem:

    http://lists.qt-project.org/pipermail/development/2013-May/010951.html

    Any solutions or hints as to how to solve this would be extremely appreciated.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      The thing is that modifying any other data role in a sql model doesn't make sense: the idea of the QSqlTableModel and QSqlQueryModel is to interact with a database which doesn't care about the background color.

      For this kind of modification you should have a look at "QIdentityProxyModel":http://qt-project.org/doc/qt-4.8/qidentityproxymodel.html

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Handbrake
        wrote on last edited by
        #3

        Hi

        Thank you for your reply. I suppose your argument makes sense--however, I wish it would be explicitly documented (any idea where Qt's Bugzilla is these days? https://bugreports.qt-project.org/secure/Dashboard.jspa maybe? So I can file a documentation bug).

        I will take a look at QIdentityProxyModel. Many thanks again!

        1 Reply Last reply
        0
        • H Offline
          H Offline
          Handbrake
          wrote on last edited by
          #4

          P.S.: I had to AdBlock *.addthis.com to be able to post, otherwise it would just hang forever.

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            yep that's the correct place for bugreports ;)

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • H Offline
              H Offline
              Handbrake
              wrote on last edited by
              #6

              [quote author="raven-worx" date="1376481343"]yep that's the correct place for bugreports ;)[/quote]

              Ta much! :)

              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