Meta-object System in QT (i): A brief description of Qvariant

Source: Internet
Author: User

Meta-object System in QT (i): A brief description of the Qvariant collection
Original articles, reproduced please indicate the source, thank you.
Author: Qing Lin, blog name: Flying Air and static crossing


Qvariant can represent most of the types in Qt, which is somewhat similar to the variant type in Pascal or the void type in C, although its use is similar to the Union in C, and its implementation is also using Union, In the Qvariant.h header file, we can see this definition:

View Plaincopy to Clipboardprint?
Class Q_core_export Qvariant
{
Public
Enum Type {
Invalid = 0,

Bool = 1,
Int = 2,
UInt = 3,
Longlong = 4,
ULONGLONG = 5,
......
usertype = 127,
#ifdef Qt3_support
IconSet = Icon,
CString = ByteArray,
Pointarray = Polygon,
#endif
Lasttype = 0xFFFFFFFF//need this so, GCC >= 3.4 allocates, bits for Type
};

inline qvariant ();
...............
}
Class Q_core_export Qvariant
{
Public
Enum Type {
Invalid = 0,

Bool = 1,
Int = 2,
UInt = 3,
Longlong = 4,
ULONGLONG = 5,
......
usertype = 127,
#ifdef Qt3_support
IconSet = Icon,
CString = ByteArray,
Pointarray = Polygon,
#endif
Lasttype = 0xFFFFFFFF//need this so, GCC >= 3.4 allocates, bits for Type
};

inline qvariant ();
...............
}


We can return its type by using the type () member function: Type Qvariant::type () const.

To get its value, we can use a function like Tot () such as ToInt (), toString (), and so on.

The basic use is as follows:

View Plaincopy to Clipboardprint?
Qdatastream out (...);
Qvariant V (123); The variant now contains an int
int x = V.toint (); x = 123
Out << v; Writes a type tag and an int.
v = qvariant ("Hello"); The variant now contains a Qbytearray
v = qvariant (tr ("Hello")); The variant now contains a QString
int y = V.toint (); y = 0 since v cannot is converted to an int
QString s = v.tostring (); s = tr ("Hello") (see Qobject::tr ())
Out << v; Writes a type tag and a QString to out
...
Qdatastream in (...); (opening the previously written stream)
In >> v; Reads an INT variant
int z = v.toint (); z = 123
Qdebug (' Type is%s ',//prints ' type is int '
V.typename ());
v = v.toint () + 100; The variant now hold the value 223
v = qvariant (Qstringlist ());
Qdatastream out (...);
Qvariant V (123); The variant now contains an int
int x = V.toint (); x = 123
Out << v; Writes a type tag and an int.
v = qvariant ("Hello"); The variant now contains a Qbytearray
v = qvariant (tr ("Hello")); The variant now contains a QString
int y = V.toint (); y = 0 since v cannot is converted to an int
QString s = v.tostring (); s = tr ("Hello") (see Qobject::tr ())
Out << v; Writes a type tag and a QString to out
...
Qdatastream in (...); (opening the previously written stream)
In >> v; Reads an INT variant
int z = v.toint (); z = 123
Qdebug (' Type is%s ',//prints ' type is int '
V.typename ());
v = v.toint () + 100; The variant now hold the value 223
v = qvariant (Qstringlist ());

Qviriant supports the use of NULL values, you can define an empty qviriant and assign it later.

View Plaincopy to Clipboardprint?
Qvariant x, Y (QString ()), Z (QString (""));
X.convert (Qvariant::int);
X.isnull () = = True
Y.isnull () = = True, z.isnull () = = False
Y.isempty () = = True, z.isempty () = = True
Qvariant x, Y (QString ()), Z (QString (""));
X.convert (Qvariant::int);
X.isnull () = = True
Y.isnull () = = True, z.isnull () = = False
Y.isempty () = = True, z.isempty () = = True


Qvariant is defined in the Qtcore library, and Qvariant.h is in the Qtcore directory, so it does not provide functions like ToInt (), toString () to convert types in Qtgui, such as Qcolor, Qimage and Qpixmap and so on. But you can use Qvariant::value () or qvariantvalue () template functions to convert.

View Plaincopy to Clipboardprint?
Qvariant variant;
...
Qcolor color = variant.value<qcolor> ();
Qvariant variant;
...
Qcolor color = variant.value<qcolor> ();

The opposite assignment can do this:

View Plaincopy to Clipboardprint?
Qcolor color = palette (). background (). color ();
Qvariant variant = color;
Qcolor color = palette (). background (). color ();
Qvariant variant = color;


You can use Canconvert () to determine if you can convert.

This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/fjb2080/archive/2009/12/09/4972915.aspx

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.