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 QObject::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).
.PP
Note that three data types supported by QVariant are explicitly shared, namely QImage, 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.
.PP
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
.br
v = QVariant(tr("hello"));// The variant now contains a QString
.br
int y = v.toInt(); // y = 0 since v cannot be converted to an int
.br
QString s = v.toString(); // s = tr("hello") (see QObject::tr())
.br
out << v; // Writes a type tag and a QString to out
.br
...
.br
QDataStream in(...); // (opening the previously written stream)
v.asInt() += 100; // The variant now hold the value 223.
.br
v = QVariant( QStringList() );
.br
v.asStringList().append( "Hello" );
.br
.fi
.PP
You can even store QValueList<QVariant>s and QMap<QString,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.
.PP
QVariant also supports the notion of NULL values, where you have a defined type with no value set.
Constructs a new variant with a boolean value, \fIval\fR. The integer argument is a dummy, necessary for compatibility with some compilers.
.SH "QVariant::QVariant ( double val )"
Constructs a new variant with a floating point value, \fIval\fR.
.SH "QVariant::QVariant ( QSizePolicy val )"
Constructs a new variant with a size policy value, \fIval\fR.
.SH "QVariant::QVariant ( const QVariant & p )"
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. QImage is.
.SH "QVariant::QVariant ( QDataStream & s )"
Reads the variant from the data stream, \fIs\fR.
.SH "QVariant::QVariant ( const QString & val )"
Constructs a new variant with a string value, \fIval\fR.
.SH "QVariant::QVariant ( const QCString & val )"
Constructs a new variant with a C-string value, \fIval\fR.
.PP
If you want to modify the QCString after you've passed it to this constructor, we recommend passing a deep copy (see QCString::copy()).
.SH "QVariant::QVariant ( const char * val )"
Constructs a new variant with a C-string value of \fIval\fR if \fIval\fR is non-null. The variant creates a deep copy of \fIval\fR.
.PP
If \fIval\fR is null, the resulting variant has type Invalid.
.SH "QVariant::QVariant ( const QStringList & val )"
Constructs a new variant with a string list value, \fIval\fR.
.SH "QVariant::QVariant ( const QFont & val )"
Constructs a new variant with a font value, \fIval\fR.
.SH "QVariant::QVariant ( const QPixmap & val )"
Constructs a new variant with a pixmap value, \fIval\fR.
.SH "QVariant::QVariant ( const QImage & val )"
Constructs a new variant with an image value, \fIval\fR.
.PP
Because QImage is explicitly shared, you may need to pass a deep copy to the variant using QImage::copy(), e.g. if you intend changing the image you've passed later on.
.SH "QVariant::QVariant ( const QBrush & val )"
Constructs a new variant with a brush value, \fIval\fR.
.SH "QVariant::QVariant ( const QPoint & val )"
Constructs a new variant with a point value, \fIval\fR.
.SH "QVariant::QVariant ( const QRect & val )"
Constructs a new variant with a rect value, \fIval\fR.
.SH "QVariant::QVariant ( const QSize & val )"
Constructs a new variant with a size value, \fIval\fR.
.SH "QVariant::QVariant ( const QColor & val )"
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.
.SH "QVariant::QVariant ( const QValueList<QVariant> & val )"
Constructs a new variant with a list value, \fIval\fR.
.SH "QVariant::QVariant ( const QMap<QString, QVariant> & val )"
Constructs a new variant with a map of QVariants, \fIval\fR.
.SH "QVariant::QVariant ( int val )"
Constructs a new variant with an integer value, \fIval\fR.
.SH "QVariant::QVariant ( uint val )"
Constructs a new variant with an unsigned integer 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.
.PP
Returns a reference to the stored string.
.PP
See also toCString().
.SH "QColor & QVariant::asColor ()"
Tries to convert the variant to hold a QColor value. If that is not possible the variant is set to an invalid color.
.PP
Returns a reference to the stored color.
.PP
See also toColor() and QColor::isValid().
.SH "QColorGroup & QVariant::asColorGroup ()"
Tries to convert the variant to hold a QColorGroup value. If that is not possible the variant is set to a color group of all black colors.
.PP
Returns a reference to the stored color group.
.PP
See also toColorGroup().
.SH "QCursor & QVariant::asCursor ()"
Tries to convert the variant to hold a QCursor value. If that is not possible the variant is set to a default arrow cursor.
.PP
Returns a reference to the stored cursor.
.PP
See also toCursor().
.SH "QDate & QVariant::asDate ()"
Tries to convert the variant to hold a QDate value. If that is not possible then the variant is set to an invalid date.
.PP
Returns a reference to the stored date.
.PP
See also toDate().
.SH "QDateTime & QVariant::asDateTime ()"
Tries to convert the variant to hold a QDateTime value. If that is not possible then the variant is set to an invalid date/time.
.PP
Returns a reference to the stored date/time.
.PP
See also toDateTime().
.SH "double & QVariant::asDouble ()"
Returns the variant's value as double reference.
.SH "QFont & QVariant::asFont ()"
Tries to convert the variant to hold a QFont. If that is not possible the variant is set to the application's default font.
.PP
Returns a reference to the stored font.
.PP
See also toFont().
.SH "QIconSet & QVariant::asIconSet ()"
Tries to convert the variant to hold a QIconSet value. If that is not possible the variant is set to an empty iconset.
.PP
Returns a reference to the stored iconset.
.PP
See also toIconSet().
.SH "QImage & QVariant::asImage ()"
Tries to convert the variant to hold an image value. If that is not possible the variant is set to a null image.
.PP
Returns a reference to the stored image.
.PP
See also toImage().
.SH "int & QVariant::asInt ()"
Returns the variant's value as int reference.
.SH "QKeySequence & QVariant::asKeySequence ()"
Tries to convert the variant to hold a QKeySequence value. If that is not possible then the variant is set to an empty key sequence.
.PP
Returns a reference to the stored key sequence.
.PP
See also toKeySequence().
.SH "QValueList<QVariant> & QVariant::asList ()"
Returns the variant's value as variant list reference.
.PP
Note that if you want to iterate over the list, you should iterate over a copy, e.g.
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).
Returns the variant as a QDateTime if the variant can be cast to DateTime; otherwise returns an invalid QDateTime.
.PP
Note that if the type() is String, CString or ByteArray an invalid QDateTime will be returned if the string cannot be parsed as a Qt::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 QString; otherwise returns an empty list.
.PP
Note that if you want to iterate over the list, you should iterate over a copy, e.g.
.PP
.nf
.br
QStringList list = myVariant.toStringList();
.br
QStringList::Iterator it = list.begin();
.br
while( it != list.end() ) {
.br
myProcessing( *it );
.br
++it;
.br
}
.br
.fi
.PP
See also asStringList().
.SH "const QTime QVariant::toTime () const"
Returns the variant as a QTime if the variant can be cast to Time; otherwise returns an invalid date.
.PP
Note that if the type() is String, CString or ByteArray an invalid time will be returned if the string cannot be parsed as a Qt::ISODate format time.
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.
.SH "const char * QVariant::typeName () const"
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", "QString", or "QValueList<QVariant>". An Invalid variant returns 0.
.SH "const char * QVariant::typeToName ( Type typ )\fC [static]\fR"
Converts the enum representation of the storage type, \fItyp\fR, to