Problem with QStringListModel and data for Qt::UserRole
-
wrote on 2 Aug 2016, 08:51 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; }
-
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; }
@Wurgl
QStringListModel::setData() only handles DisplayRole and EditRole.
The other roles are simply ignored. -
wrote on 2 Aug 2016, 09:21 last edited by
Just checked the documentation, but (so far) did not find any hint there. Shouldn't this be in the documentation?
-
Just checked the documentation, but (so far) did not find any hint there. Shouldn't this be in the documentation?
Moderatorswrote on 2 Aug 2016, 09:58 last edited by raven-worx 8 Feb 2016, 10:00@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) -
wrote on 2 Aug 2016, 11:01 last edited by
Solved for me … thanks!
-
Hi,
Yes, please open a report for a documentation update.
1/6