Qvariant The pointer data cannot be saved by default because the Qvariant constructor with the void * parameter is private.
However, with the meta-type mechanism provided by QT, arbitrary pointers can be stored in the qvariant.
You need to register the type with the Q_declare_metatype macro.
| 123 |
q_declare_metatype(qstandarditemmodel*) |
After that, you can use Qvariant::fromvalue to store data and use Qvariant::value to get the data.
| 12345 |
return Qvariant::fromvalue ( model_) //.... qstandarditemmodel* Model = some_value< Span class= "Crayon-sy". value ( |
Using Qvariant and Qt::itemdatarole, which identifies the data types in your project, makes it easy to store arbitrary data in a predefined model provided by QT.
For example, use different role in the same qstandarditem to store multiple data.
Custom Data Roles
| 12345678 |
enum customitemrole { levelmodelrole = Qt::userrole + + , timemodelrole = Qt::userrole + 1010, filemodelrole = Qt::userrole + 1020 /c21> }; |
Storing data for a specific role
| 123 |
type_of_level_item-setData(qvariant::fromvalue(level_list_model) ,levelmodelrole); |
Get data for a specific role
| 123 |
qvariant data = Item, Data(Qt::levelmodelrole); |
Because Qstandarditemmodel creates tree structures in behavioral units, for more complex tree structures, you can use a custom datarole to store data.
Qvariant Saving pointer data