Because C++ forbids unions from including types that have non-default constructors or destructors, most interesting TQt classes cannot be used in unions. Without QVariant, this would be a problem for TQObject::property() and for database work, etc.
A QVariant object holds a single value of a single type() at a time. (Some type()s are multi-valued, for example a string list.) You can find out what type, T, the variant holds, convert it to a different type using one of the asT() functions, e.g. asSize(), get its value using one of the toT() functions, e.g. toSize(), and check whether the type can be converted to a particular type using canCast().
.PP
The methods named toT() (for any supported T, see the Type documentation for a list) are const. If you ask for the stored type, they return a copy of the stored object. If you ask for a type that can be generated from the stored type, toT() copies and converts and leaves the object itself unchanged. If you ask for a type that cannot be generated from the stored type, the result depends on the type (see the function documentation for details).
Note that three data types supported by QVariant are explicitly shared, namely TQImage, QPointArray, and QCString, and in these cases the toT() methods return a shallow copy. In almost all cases you must make a deep copy of the returned values before modifying them.
The asT() functions are not const. They do conversion like the toT() methods, set the variant to hold the converted value, and return a reference to the new contents of the variant.
.PP
Here is some example code to demonstrate the use of QVariant:
.PP
.nf
.br
QDataStream out(...);
.br
QVariant v(123); // The variant now contains an int
.br
int x = v.toInt(); // x = 123
.br
out << v; // Writes a type tag and an int to out
.br
v = QVariant("hello"); // The variant now contains a QCString
You can even store TQValueList<QVariant>s and TQMap<TQString,QVariant>s in a variant, so you can easily construct arbitrarily complex data structures of arbitrary types. This is very powerful and versatile, but may prove less memory and speed efficient than storing specific types in standard data structures.
Constructs a copy of the variant, \fIp\fR, passed as the argument to this constructor. Usually this is a deep copy, but a shallow copy is made if the stored data type is explicitly shared, as e.g. TQImage is.
Because TQImage is explicitly shared, you may need to pass a deep copy to the variant using TQImage::copy(), e.g. if you intend changing the image you've passed later on.
Constructs a new variant with a color value, \fIval\fR.
.SH "QVariant::QVariant ( const QPalette & val )"
Constructs a new variant with a color palette value, \fIval\fR.
.SH "QVariant::QVariant ( const QColorGroup & val )"
Constructs a new variant with a color group value, \fIval\fR.
.SH "QVariant::QVariant ( const QIconSet & val )"
Constructs a new variant with an icon set value, \fIval\fR.
.SH "QVariant::QVariant ( const QPointArray & val )"
Constructs a new variant with a point array value, \fIval\fR.
.PP
Because QPointArray is explicitly shared, you may need to pass a deep copy to the variant using QPointArray::copy(), e.g. if you intend changing the point array you've passed later on.
.SH "QVariant::QVariant ( const QRegion & val )"
Constructs a new variant with a region value, \fIval\fR.
.SH "QVariant::QVariant ( const QBitmap & val )"
Constructs a new variant with a bitmap value, \fIval\fR.
.SH "QVariant::QVariant ( const QCursor & val )"
Constructs a new variant with a cursor value, \fIval\fR.
.SH "QVariant::QVariant ( const QDate & val )"
Constructs a new variant with a date value, \fIval\fR.
.SH "QVariant::QVariant ( const QTime & val )"
Constructs a new variant with a time value, \fIval\fR.
.SH "QVariant::QVariant ( const QDateTime & val )"
Constructs a new variant with a date/time value, \fIval\fR.
.SH "QVariant::QVariant ( const QByteArray & val )"
Constructs a new variant with a bytearray value, \fIval\fR.
.SH "QVariant::QVariant ( const QBitArray & val )"
Constructs a new variant with a bitarray value, \fIval\fR.
.SH "QVariant::QVariant ( const QKeySequence & val )"
Constructs a new variant with a key sequence value, \fIval\fR.
.SH "QVariant::QVariant ( const QPen & val )"
Constructs a new variant with a pen value, \fIval\fR.
Constructs a new variant with an unsigned long long integer value, \fIval\fR.
.SH "QVariant::~QVariant ()"
Destroys the QVariant and the contained object.
.PP
Note that subclasses that reimplement clear() should reimplement the destructor to call clear(). This destructor calls clear(), but because it is the destructor, QVariant::clear() is called rather than a subclass's clear().
.SH "QBitArray & QVariant::asBitArray ()"
Tries to convert the variant to hold a QBitArray value. If that is not possible then the variant is set to an empty bitarray.
.PP
Returns a reference to the stored bitarray.
.PP
See also toBitArray().
.SH "QBitmap & QVariant::asBitmap ()"
Tries to convert the variant to hold a bitmap value. If that is not possible the variant is set to a null bitmap.
.PP
Returns a reference to the stored bitmap.
.PP
See also toBitmap().
.SH "bool & QVariant::asBool ()"
Returns the variant's value as bool reference.
.SH "QBrush & QVariant::asBrush ()"
Tries to convert the variant to hold a brush value. If that is not possible the variant is set to a default black brush.
.PP
Returns a reference to the stored brush.
.PP
See also toBrush().
.SH "QByteArray & QVariant::asByteArray ()"
Tries to convert the variant to hold a QByteArray value. If that is not possible then the variant is set to an empty bytearray.
.PP
Returns a reference to the stored bytearray.
.PP
See also toByteArray().
.SH "QCString & QVariant::asCString ()"
Tries to convert the variant to hold a string value. If that is not possible the variant is set to an empty string.
Returns the variant's value as unsigned long long reference.
.SH "bool QVariant::canCast ( Type t ) const"
Returns TRUE if the variant's type can be cast to the requested type, \fIt\fR. Such casting is done automatically when calling the toInt(), toBool(), ... or asInt(), asBool(), ... methods.
.PP
The following casts are done automatically: <center>.nf
.TS
l - l. Type Automatically Cast To Bool Double, Int, UInt, LongLong, ULongLong, String, CString, ByteArray Color String. CString. ByteArray Date String, CString, ByteArray, DateTime DateTime String, CString, ByteArray, Date, Time Double String, CString, ByteArray, Int, Bool, UInt, LongLong, ULongLong Font String, CString, ByteArray Int String, CString, ByteArray, Double, Bool, UInt, LongLong, ULongLong, KeySequence LongLong String, CString, ByteArray, Double, Bool, UInt, LongLong, ULongLong, KeySequence ULongLong String, CString, ByteArray, Double, Bool, UInt, LongLong, ULongLong, KeySequence List StringList (if the list contains only strings or something that can be cast to a string) String CString, ByteArray, CString, Int, UInt, Bool, Double, Date, Time, DateTime, KeySequence, Font, Color CString String, ByteArray, Int, UInt, Bool, Double, Date, ULongLong, LongLong ByteArray String, CString, Int, UInt, Bool, Double, Date, ULongLong, LongLong StringList List Time String Int String, CString, ByteArray, Double, Bool, UInt, LongLong, ULongLong, KeySequence KeySequence
.TE
.fi
</center>
.SH "bool QVariant::cast ( Type t )"
Casts the variant to the requested type. If the cast cannot be done, the variant is set to the default value of the requested type (e.g. an empty string if the requested type \fIt\fR is QVariant::String, an empty point array if the requested type \fIt\fR is QVariant::PointArray, etc). Returns TRUE if the current type of the variant was successfully cast; otherwise returns FALSE.
.PP
See also canCast().
.SH "void QVariant::clear ()"
Convert this variant to type Invalid and free up any resources used.
.SH "bool QVariant::isNull () const"
Returns TRUE if this is a NULL variant, FALSE otherwise.
.SH "bool QVariant::isValid () const"
Returns TRUE if the storage type of this variant is not QVariant::Invalid; otherwise returns FALSE.
\fBThis function is obsolete.\fR It is provided to keep old source working. We strongly advise against using it in new code.
.PP
Returns an iterator to the item in the map with \fIkey\fR as key, if the variant's type is appropriate and \fIkey\fR is a valid key; otherwise returns a null iterator.
.SH "Type QVariant::nameToType ( const char * name )\fC [static]\fR"
Converts the string representation of the storage type given in \fIname\fR, to its enum representation.
.PP
If the string representation cannot be converted to any enum representation, the variant is set to Invalid.
.SH "bool QVariant::operator!= ( const QVariant & v ) const"
Compares this QVariant with \fIv\fR and returns TRUE if they are not equal; otherwise returns FALSE.
Returns the variant as a QBitArray if the variant has type() BitArray; otherwise returns an empty bitarray.
.PP
See also asBitArray().
.SH "const QBitmap QVariant::toBitmap () const"
Returns the variant as a QBitmap if the variant has type() Bitmap; otherwise returns a null QBitmap.
.PP
See also asBitmap().
.SH "bool QVariant::toBool () const"
Returns the variant as a bool if the variant can be cast to Bool; otherWise returns FALSE.
.PP
Returns TRUE if the variant has a numeric type and its value is non-zero, or if the variant has type String, ByteArray or CString and its lower-case content is not empty, "0" or "false"; otherwise returns FALSE.
.PP
See also asBool() and canCast().
.SH "const QBrush QVariant::toBrush () const"
Returns the variant as a QBrush if the variant has type() Brush; otherwise returns a default brush (with all black colors).
Note that if the type() is String, CString or ByteArray an invalid QDateTime will be returned if the string cannot be parsed as a TQt::ISODate format date/time.
Returns the variant as a QStringList if the variant has type() StringList or List of a type that can be converted to TQString; otherwise returns an empty list.
Returns the variant as as an unsigned long long int if the variant can be cast to ULongLong; otherwise returns 0.
.PP
If \fIok\fR is non-null: \fI*ok\fR is set to TRUE if the value could be converted to an int; otherwise \fI*ok\fR is set to FALSE.
.PP
See also asULongLong() and canCast().
.SH "Type QVariant::type () const"
Returns the storage type of the value stored in the variant. Usually it's best to test with canCast() whether the variant can deliver the data type you are interested in.
Returns the name of the type stored in the variant. The returned strings describe the C++ datatype used to store the data: for example, "QFont", "TQString", or "TQValueList<QVariant>". An Invalid variant returns 0.