Qvariant (equivalent to the object in Java, the role of a data type "erase" can be registered using Q_declare_metatype)

Source: Internet
Author: User

=qvariant=

[%
This type is equivalent to the object in Java, which encapsulates most of the data types provided by QT and acts as a data type "erase". For example, if our table cell can be a string or an int or a color value, how can so many types return? As a result, QT provides this qvariant type, you can put many of these types are stored in the need to use a series of to function to take out. For example, if you wrap int as a qvariant, use the Qvariant::toint () to take it out again. It should be noted here that the Qvariant type must be put in and out of the corresponding, you have to put an int must be taken out by int, not with ToString (), QT will not help you automatically convert.
The core of the data is nothing more than a union, and a type of a marker: the integer 123 is passed, then it union stores the integer 123, and the type flag int; If the string is passed, the Union stores a pointer to the string, and the type flag qstring.
Qvariant belongs to QT core module, which belongs to one of Qt's underlying cores, ACTIVEQT, Qtscript, qtdeclarative and so on are heavily dependent on qvariant.

Qvariant can store many QT data types, including Qbrush, Qcolor, Qcursor, Qdatetime, Qfont, Qkeysequence, Qpalette, Qpen, Qpixmap, Qpoint, QRect, Qregion, Qsize, and qstring, and there are basic C + + types, such as int, float, and so on. Qvariant can also hold many collection types, such as qmap<qstring, Qvariant&gt, Qstringlist and Qlist. Item view classes, database modules and qsettings are heavily used in the Qvariant class to facilitate our reading and writing of data.
Qvariant can also perform nested storage, such as

Qmap<qstring, qvariant> Pearmap;
Pearmap[\ "standard\"] = 1.95;
Pearmap[\ "organic\"] = 2.25;

Qmap<qstring, qvariant> Fruitmap;
Fruitmap[\ "orange\"] = 2.10;
Fruitmap[\ "pineapple\"] = 3.85;
Fruitmap[\ "pear\"] = Pearmap;
Qvariant is used to build QT Meta-object and is therefore part of the Qtcore. Of course, we can also use it in GUI modules, such as

Qicon icon (\ "open.png\");
Qvariant variant = icon;
Other function
Qicon icon = variant.value<qicon> ();
We used the value () template function to get the data stored in the qvariant. This function is also applicable in non-GUI data, but in non-GUI modules, we usually use a series of toint () to ... () function, such as ToString ().
You can also customize the storage type of qvariant if you feel that Qvariant provides too few types of storage data. The data type stored by the qvariant needs to have a default constructor and a copy constructor. In order to implement this function, you must first use the Q_declare_metatype () macro. This macro is typically placed under the header file of the class declaration:

Q_declare_metatype (Businesscard)
Then we can use:

Businesscard Businesscard;
Qvariant variant = Qvariant::fromvalue (Businesscard);
// ...
if (variant.canconvert<businesscard> ()) {
Businesscard card = variant.value<businesscard> ();
// ...
}
Because of the compiler limitations of VC 6, these template functions cannot be used, if you use this compiler, you need to use Qvariantfromvalue (), Qvariantvalue () and Qvariantcanconvert () three macros.
If your custom data type overrides the << and >> operators, you can use it directly in Qdatastream. First, however, you need to use Qregistermetatypestreamoperators (). Macro to register. This allows qsettings to manipulate the data using an operator, such as
%]

---

Reprint please indicate this article title and Link: "Qvariant"

Http://www.iybin.com/?post=13

Qvariant (equivalent to the object in Java, the role of a data type "erase" can be registered using Q_declare_metatype)

Related Article

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.