Problem with QStringListModel and data for Qt::UserRole



  • 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;
    }
    

  • Moderators

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



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


  • Moderators

    @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)



  • Solved for me … thanks!


  • Lifetime Qt Champion

    Hi,

    Yes, please open a report for a documentation update.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.