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. Problem with QStringListModel and data for Qt::UserRole

Problem with QStringListModel and data for Qt::UserRole

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 2.3k Views
  • 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.
  • W Offline
    W Offline
    Wurgl
    wrote on last edited by
    #1

    I tried a QStringListModel for filling some model for a QComboBox, but failed with setting additional data with the role Qt::UserRole (same with Qt::UserRole+1). This is somehow unexpected.

    As a solution, I could change it to QStandardItemModel, but I thought to use this time something different.

    Qt-Version is 5.4.2 (same with Qt 4.8.2)

    An example-snipplet to reproduce:

    #include <QStringListModel>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
      QStringListModel *m = new QStringListModel();
      m->insertRow(0);
      qDebug() << "m has" << m->rowCount() << "rows";
    
      m->setData(m->index(0, 0), "Text"); // works fine
      qDebug() << "item text is" << m->data(m->index(0, 0), Qt::EditRole);
    
      m->setData(m->index(0, 0), "Data", Qt::UserRole); // does not work as expected
      qDebug() << "item data is" << m->data(m->index(0, 0), Qt::UserRole);
    
      m->setData(m->index(0, 0), "modified Text"); // works fine
      qDebug() << "item text is" << m->data(m->index(0, 0), Qt::EditRole);
      return 0;
    }
    
    raven-worxR 1 Reply Last reply
    0
    • W Wurgl

      I tried a QStringListModel for filling some model for a QComboBox, but failed with setting additional data with the role Qt::UserRole (same with Qt::UserRole+1). This is somehow unexpected.

      As a solution, I could change it to QStandardItemModel, but I thought to use this time something different.

      Qt-Version is 5.4.2 (same with Qt 4.8.2)

      An example-snipplet to reproduce:

      #include <QStringListModel>
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
        QStringListModel *m = new QStringListModel();
        m->insertRow(0);
        qDebug() << "m has" << m->rowCount() << "rows";
      
        m->setData(m->index(0, 0), "Text"); // works fine
        qDebug() << "item text is" << m->data(m->index(0, 0), Qt::EditRole);
      
        m->setData(m->index(0, 0), "Data", Qt::UserRole); // does not work as expected
        qDebug() << "item data is" << m->data(m->index(0, 0), Qt::UserRole);
      
        m->setData(m->index(0, 0), "modified Text"); // works fine
        qDebug() << "item text is" << m->data(m->index(0, 0), Qt::EditRole);
        return 0;
      }
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Wurgl
      QStringListModel::setData() only handles DisplayRole and EditRole.
      The other roles are simply ignored.

      --- 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
      1
      • W Offline
        W Offline
        Wurgl
        wrote on last edited by
        #3

        Just checked the documentation, but (so far) did not find any hint there. Shouldn't this be in the documentation?

        raven-worxR 1 Reply Last reply
        0
        • W Wurgl

          Just checked the documentation, but (so far) did not find any hint there. Shouldn't this be in the documentation?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @Wurgl said:

          Just checked the documentation, but (so far) did not find any hint there. Shouldn't this be in the documentation?

          yes it should probably.
          If you like you can create a request in the bugtracker (for "Documentation" component)

          --- 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
          • W Offline
            W Offline
            Wurgl
            wrote on last edited by
            #5

            Solved for me … thanks!

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

              Hi,

              Yes, please open a report for a documentation update.

              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

              • Login

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