Qvariant Saving pointer data

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.