Background
In my model I have to keep in one item's data, a reference to another item.
The documentation of QPersistentModelIndex
says:
A
QPersistentModelIndex
is a model index that can be stored by an application, and later used to access information in a model.
QStandardItemModel::setData
accepts QVariant
as a value, which in turn can be constructed from QPersistentModelIndex
, so I am able to write something like this:
...
auto *model = new QStandardItemModel();
auto *referencedItem = new QStandardItem("Test");
auto *item = new QStandardItem();
model->appendRow(referencedItem);
item->setData(QPersistentModelIndex(referencedItem->index()), Qt::UserRole);
// the next line prints "Test"
qDebug() << item->data(Qt::UserRole).toPersistentModelIndex().data().toString();
...
The code compiles and prints:
Test
The accepted answers to another topics, e.g. Storing persistent information about items in view and How to update a QTableView cell with a QCombobox selection?, also suggest the use of QPersistentModelIndex
.
Problem
The documentation of QPersistentModelIndex
also says:
Note: You cannot store a
QStandardItemModel
'sQPersistentModelIndex
in one of the model's items.
Does this mean, that using QPersistentModelIndex
like in the code above, i.e.:
item->setData(QPersistentModelIndex(referencedItem->index()), Qt::UserRole);
is wrong and why this should not be done?