Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Problem with QStringListModel and data for Qt::UserRole

    General and Desktop
    3
    6
    1689
    Loading More Posts
    • 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
      Wurgl last edited by

      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-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @Wurgl last edited by

        @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 Reply Quote 1
        • W
          Wurgl last edited by

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

          raven-worx 1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators @Wurgl last edited by raven-worx

            @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 Reply Quote 0
            • W
              Wurgl last edited by

              Solved for me … thanks!

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 0
                • First post
                  Last post