QStandardItemModel::setItemData() removes data from roles where it shouldn't
I use a QStandardItemModel with a QTreeView. The QStandardItem's inside the model have some userdefined roles (Qt::UserRole + ...). I use
QStandardItemModel::setItemData(const QModelIndex & index, const QMap<int, QVariant> & roles)
to set multiple roles at the same time. The documentation of this function explains that roles not mentioned in the QMap of roles will NOT be modified. But after a call to setItemData() i lost the data of other roles.
Test: read data from a user role before and after a call to setItemData()
My code :
// ... do stuff then... QMap<int, QVariant> mapRoles; mapRoles.insert( Qt::DisplayRole, strValue ); mapRoles.insert( DATA_MODEL_ROLE_ITEM_DATA, varValue ); // DEBUG // get data from user role "item_format" auto listFormat = iter->sibling( currentRow, DATA_MODEL_COLUMN_VALUE ).data( DATA_MODEL_ROLE_ITEM_FORMAT ).toList(); QModelIndex idxItemValue = iter->sibling( currentRow, DATA_MODEL_COLUMN_VALUE ); // set data of roles to item m_dataStoreModel->setItemData( idxItemValue, mapRoles ); // DEBUG listFormat.clear(); // get data from user role "item-format" listFormat = iter->sibling( currentRow, DATA_MODEL_COLUMN_VALUE ).data( DATA_MODEL_ROLE_ITEM_FORMAT ).toList();
The result is that the first time I get the data from the user role to the 'listFormat' I can see the elements of the list. The second time I get an empty list.
I looked at the implementation of QStandardItemPrivate::setItemData() (qstandarditemmodel.cpp) and there I can't see an append-like functionality.
But maybe i missed something or doing something wrong? Or is there a bug in the setItemData() implementation?
If what the function does differs from the documentation, then you have found a bug. Please report it on (including a minimal compilable example) Qt Jira. Additionally, if it used to work in previous versions of Qt, this is a regression and if marked as such it should get much more attention.
thx, I added a report to Qt Jira.