QVariant

Section: Misc. Reference Manual Pages (3qt)
Updated: 2 February 2007
Index Return to Main Contents
 

NAME

QVariant - Acts like a union for the most common Qt data types  

SYNOPSIS

#include <qvariant.h>

 

Public Members


enum Type { Invalid, Map, List, String, StringList, Font, Pixmap, Brush, Rect, Size, Color, Palette, ColorGroup, IconSet, Point, Image, Int, UInt, Bool, Double, CString, PointArray, Region, Bitmap, Cursor, SizePolicy, Date, Time, DateTime, ByteArray, BitArray, KeySequence, Pen, LongLong, ULongLong }

QVariant ()

~QVariant ()

QVariant ( const QVariant & p )

QVariant ( QDataStream & s )

QVariant ( const QString & val )

QVariant ( const QCString & val )

QVariant ( const char * val )

QVariant ( const QStringList & val )

QVariant ( const QFont & val )

QVariant ( const QPixmap & val )

QVariant ( const QImage & val )

QVariant ( const QBrush & val )

QVariant ( const QPoint & val )

QVariant ( const QRect & val )

QVariant ( const QSize & val )

QVariant ( const QColor & val )

QVariant ( const QPalette & val )

QVariant ( const QColorGroup & val )

QVariant ( const QIconSet & val )

QVariant ( const QPointArray & val )

QVariant ( const QRegion & val )

QVariant ( const QBitmap & val )

QVariant ( const QCursor & val )

QVariant ( const QDate & val )

QVariant ( const QTime & val )

QVariant ( const QDateTime & val )

QVariant ( const QByteArray & val )

QVariant ( const QBitArray & val )

QVariant ( const QKeySequence & val )

QVariant ( const QPen & val )

QVariant ( const QValueList<QVariant> & val )

QVariant ( const QMap<QString, QVariant> & val )

QVariant ( int val )

QVariant ( uint val )

QVariant ( Q_LLONG val )

QVariant ( Q_ULLONG val )

QVariant ( bool val, int )

QVariant ( double val )

QVariant ( QSizePolicy val )

QVariant & operator= ( const QVariant & variant )

bool operator== ( const QVariant & v ) const

bool operator!= ( const QVariant & v ) const

Type type () const

const char * typeName () const

bool canCast ( Type t ) const

bool cast ( Type t )

bool isValid () const

bool isNull () const

void clear ()

const QString toString () const

const QCString toCString () const

const QStringList toStringList () const

const QFont toFont () const

const QPixmap toPixmap () const

const QImage toImage () const

const QBrush toBrush () const

const QPoint toPoint () const

const QRect toRect () const

const QSize toSize () const

const QColor toColor () const

const QPalette toPalette () const

const QColorGroup toColorGroup () const

const QIconSet toIconSet () const

const QPointArray toPointArray () const

const QBitmap toBitmap () const

const QRegion toRegion () const

const QCursor toCursor () const

const QDate toDate () const

const QTime toTime () const

const QDateTime toDateTime () const

const QByteArray toByteArray () const

const QBitArray toBitArray () const

const QKeySequence toKeySequence () const

const QPen toPen () const

int toInt ( bool * ok = 0 ) const

uint toUInt ( bool * ok = 0 ) const

Q_LLONG toLongLong ( bool * ok = 0 ) const

Q_ULLONG toULongLong ( bool * ok = 0 ) const

bool toBool () const

double toDouble ( bool * ok = 0 ) const

const QValueList<QVariant> toList () const

const QMap<QString, QVariant> toMap () const

QSizePolicy toSizePolicy () const

QValueListConstIterator<QString> stringListBegin () const (obsolete)

QValueListConstIterator<QString> stringListEnd () const (obsolete)

QValueListConstIterator<QVariant> listBegin () const (obsolete)

QValueListConstIterator<QVariant> listEnd () const (obsolete)

QMapConstIterator<QString, QVariant> mapBegin () const (obsolete)

QMapConstIterator<QString, QVariant> mapEnd () const (obsolete)

QMapConstIterator<QString, QVariant> mapFind ( const QString & key ) const (obsolete)

QString & asString ()

QCString & asCString ()

QStringList & asStringList ()

QFont & asFont ()

QPixmap & asPixmap ()

QImage & asImage ()

QBrush & asBrush ()

QPoint & asPoint ()

QRect & asRect ()

QSize & asSize ()

QColor & asColor ()

QPalette & asPalette ()

QColorGroup & asColorGroup ()

QIconSet & asIconSet ()

QPointArray & asPointArray ()

QBitmap & asBitmap ()

QRegion & asRegion ()

QCursor & asCursor ()

QDate & asDate ()

QTime & asTime ()

QDateTime & asDateTime ()

QByteArray & asByteArray ()

QBitArray & asBitArray ()

QKeySequence & asKeySequence ()

QPen & asPen ()

int & asInt ()

uint & asUInt ()

Q_LLONG & asLongLong ()

Q_ULLONG & asULongLong ()

bool & asBool ()

double & asDouble ()

QValueList<QVariant> & asList ()

QMap<QString, QVariant> & asMap ()

QSizePolicy & asSizePolicy ()
 

Static Public Members


const char * typeToName ( Type typ )

Type nameToType ( const char * name )
 

DESCRIPTION

The QVariant class acts like a union for the most common Qt data types.

Because C++ forbids unions from including types that have non-default constructors or destructors, most interesting Qt 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().

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 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.

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.

Here is some example code to demonstrate the use of QVariant:


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 to out
v = QVariant("hello"); // The variant now contains a QCString
v = QVariant(tr("hello"));// The variant now contains a QString
int y = v.toInt(); // y = 0 since v cannot be 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.asInt() += 100; // The variant now hold the value 223.
v = QVariant( QStringList() );
v.asStringList().append( "Hello" );

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.

QVariant also supports the notion of NULL values, where you have a defined type with no value set.


QVariant x, y( QString() ), z( QString("") );
x.asInt();
// x.isNull() == TRUE, y.isNull() == TRUE, z.isNull() == FALSE

See the Collection Classes.

See also Miscellaneous Classes and Object Model.  

Member Type Documentation

 

QVariant::Type

This enum type defines the types of variable that a QVariant can contain.
QVariant::Invalid - no type
QVariant::BitArray - a QBitArray
QVariant::ByteArray - a QByteArray
QVariant::Bitmap - a QBitmap
QVariant::Bool - a bool
QVariant::Brush - a QBrush
QVariant::Color - a QColor
QVariant::ColorGroup - a QColorGroup
QVariant::Cursor - a QCursor
QVariant::Date - a QDate
QVariant::DateTime - a QDateTime
QVariant::Double - a double
QVariant::Font - a QFont
QVariant::IconSet - a QIconSet
QVariant::Image - a QImage
QVariant::Int - an int
QVariant::KeySequence - a QKeySequence
QVariant::List - a QValueList<QVariant>
QVariant::LongLong - a long long
QVariant::ULongLong - an unsigned long long
QVariant::Map - a QMap<QString,QVariant>
QVariant::Palette - a QPalette
QVariant::Pen - a QPen
QVariant::Pixmap - a QPixmap
QVariant::Point - a QPoint
QVariant::PointArray - a QPointArray
QVariant::Rect - a QRect
QVariant::Region - a QRegion
QVariant::Size - a QSize
QVariant::SizePolicy - a QSizePolicy
QVariant::String - a QString
QVariant::CString - a QCString
QVariant::StringList - a QStringList
QVariant::Time - a QTime
QVariant::UInt - an unsigned int

Note that Qt's definition of bool depends on the compiler. qglobal.h has the system-dependent definition of bool.  

MEMBER FUNCTION DOCUMENTATION

 

QVariant::QVariant ()

Constructs an invalid variant.  

QVariant::QVariant ( bool val, int )

Constructs a new variant with a boolean value, val. The integer argument is a dummy, necessary for compatibility with some compilers.  

QVariant::QVariant ( double val )

Constructs a new variant with a floating point value, val.  

QVariant::QVariant ( QSizePolicy val )

Constructs a new variant with a size policy value, val.  

QVariant::QVariant ( const QVariant & p )

Constructs a copy of the variant, p, 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.  

QVariant::QVariant ( QDataStream & s )

Reads the variant from the data stream, s.  

QVariant::QVariant ( const QString & val )

Constructs a new variant with a string value, val.  

QVariant::QVariant ( const QCString & val )

Constructs a new variant with a C-string value, val.

If you want to modify the QCString after you've passed it to this constructor, we recommend passing a deep copy (see QCString::copy()).  

QVariant::QVariant ( const char * val )

Constructs a new variant with a C-string value of val if val is non-null. The variant creates a deep copy of val.

If val is null, the resulting variant has type Invalid.  

QVariant::QVariant ( const QStringList & val )

Constructs a new variant with a string list value, val.  

QVariant::QVariant ( const QFont & val )

Constructs a new variant with a font value, val.  

QVariant::QVariant ( const QPixmap & val )

Constructs a new variant with a pixmap value, val.  

QVariant::QVariant ( const QImage & val )

Constructs a new variant with an image value, val.

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.  

QVariant::QVariant ( const QBrush & val )

Constructs a new variant with a brush value, val.  

QVariant::QVariant ( const QPoint & val )

Constructs a new variant with a point value, val.  

QVariant::QVariant ( const QRect & val )

Constructs a new variant with a rect value, val.  

QVariant::QVariant ( const QSize & val )

Constructs a new variant with a size value, val.  

QVariant::QVariant ( const QColor & val )

Constructs a new variant with a color value, val.  

QVariant::QVariant ( const QPalette & val )

Constructs a new variant with a color palette value, val.  

QVariant::QVariant ( const QColorGroup & val )

Constructs a new variant with a color group value, val.  

QVariant::QVariant ( const QIconSet & val )

Constructs a new variant with an icon set value, val.  

QVariant::QVariant ( const QPointArray & val )

Constructs a new variant with a point array value, val.

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.  

QVariant::QVariant ( const QRegion & val )

Constructs a new variant with a region value, val.  

QVariant::QVariant ( const QBitmap & val )

Constructs a new variant with a bitmap value, val.  

QVariant::QVariant ( const QCursor & val )

Constructs a new variant with a cursor value, val.  

QVariant::QVariant ( const QDate & val )

Constructs a new variant with a date value, val.  

QVariant::QVariant ( const QTime & val )

Constructs a new variant with a time value, val.  

QVariant::QVariant ( const QDateTime & val )

Constructs a new variant with a date/time value, val.  

QVariant::QVariant ( const QByteArray & val )

Constructs a new variant with a bytearray value, val.  

QVariant::QVariant ( const QBitArray & val )

Constructs a new variant with a bitarray value, val.  

QVariant::QVariant ( const QKeySequence & val )

Constructs a new variant with a key sequence value, val.  

QVariant::QVariant ( const QPen & val )

Constructs a new variant with a pen value, val.  

QVariant::QVariant ( const QValueList<QVariant> & val )

Constructs a new variant with a list value, val.  

QVariant::QVariant ( const QMap<QString, QVariant> & val )

Constructs a new variant with a map of QVariants, val.  

QVariant::QVariant ( int val )

Constructs a new variant with an integer value, val.  

QVariant::QVariant ( uint val )

Constructs a new variant with an unsigned integer value, val.  

QVariant::QVariant ( Q_LLONG val )

Constructs a new variant with a long long integer value, val.  

QVariant::QVariant ( Q_ULLONG val )

Constructs a new variant with an unsigned long long integer value, val.  

QVariant::~QVariant ()

Destroys the QVariant and the contained object.

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().  

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.

Returns a reference to the stored bitarray.

See also toBitArray().  

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.

Returns a reference to the stored bitmap.

See also toBitmap().  

bool & QVariant::asBool ()

Returns the variant's value as bool reference.  

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.

Returns a reference to the stored brush.

See also toBrush().  

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.

Returns a reference to the stored bytearray.

See also toByteArray().  

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 a reference to the stored string.

See also toCString().  

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.

Returns a reference to the stored color.

See also toColor() and QColor::isValid().  

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.

Returns a reference to the stored color group.

See also toColorGroup().  

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.

Returns a reference to the stored cursor.

See also toCursor().  

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.

Returns a reference to the stored date.

See also toDate().  

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.

Returns a reference to the stored date/time.

See also toDateTime().  

double & QVariant::asDouble ()

Returns the variant's value as double reference.  

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.

Returns a reference to the stored font.

See also toFont().  

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.

Returns a reference to the stored iconset.

See also toIconSet().  

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.

Returns a reference to the stored image.

See also toImage().  

int & QVariant::asInt ()

Returns the variant's value as int reference.  

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.

Returns a reference to the stored key sequence.

See also toKeySequence().  

QValueList<QVariant> & QVariant::asList ()

Returns the variant's value as variant list reference.

Note that if you want to iterate over the list, you should iterate over a copy, e.g.


QValueList<QVariant> list = myVariant.asList();
QValueList<QVariant>::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
 

Q_LLONG & QVariant::asLongLong ()

Returns the variant's value as long long reference.  

QMap<QString, QVariant> & QVariant::asMap ()

Returns the variant's value as variant map reference.

Note that if you want to iterate over the map, you should iterate over a copy, e.g.


QMap<QString, QVariant> map = myVariant.asMap();
QMap<QString, QVariant>::Iterator it = map.begin();
while( it != map.end() ) {
myProcessing( *it );
++it;
}
 

QPalette & QVariant::asPalette ()

Tries to convert the variant to hold a QPalette value. If that is not possible the variant is set to a palette of black colors.

Returns a reference to the stored palette.

See also toString().  

QPen & QVariant::asPen ()

Tries to convert the variant to hold a QPen value. If that is not possible then the variant is set to an empty pen.

Returns a reference to the stored pen.

See also toPen().  

QPixmap & QVariant::asPixmap ()

Tries to convert the variant to hold a pixmap value. If that is not possible the variant is set to a null pixmap.

Returns a reference to the stored pixmap.

See also toPixmap().  

QPoint & QVariant::asPoint ()

Tries to convert the variant to hold a point value. If that is not possible the variant is set to a (0, 0) point.

Returns a reference to the stored point.

See also toPoint().  

QPointArray & QVariant::asPointArray ()

Tries to convert the variant to hold a QPointArray value. If that is not possible the variant is set to an empty point array.

Returns a reference to the stored point array.

See also toPointArray().  

QRect & QVariant::asRect ()

Tries to convert the variant to hold a rectangle value. If that is not possible the variant is set to an empty rectangle.

Returns a reference to the stored rectangle.

See also toRect().  

QRegion & QVariant::asRegion ()

Tries to convert the variant to hold a QRegion value. If that is not possible the variant is set to a null region.

Returns a reference to the stored region.

See also toRegion().  

QSize & QVariant::asSize ()

Tries to convert the variant to hold a QSize value. If that is not possible the variant is set to an invalid size.

Returns a reference to the stored size.

See also toSize() and QSize::isValid().  

QSizePolicy & QVariant::asSizePolicy ()

Tries to convert the variant to hold a QSizePolicy value. If that fails, the variant is set to an arbitrary (valid) size policy.  

QString & QVariant::asString ()

Tries to convert the variant to hold a string value. If that is not possible the variant is set to an empty string.

Returns a reference to the stored string.

See also toString().  

QStringList & QVariant::asStringList ()

Tries to convert the variant to hold a QStringList value. If that is not possible the variant is set to an empty string list.

Returns a reference to the stored string list.

Note that if you want to iterate over the list, you should iterate over a copy, e.g.


QStringList list = myVariant.asStringList();
QStringList::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}

See also toStringList().  

QTime & QVariant::asTime ()

Tries to convert the variant to hold a QTime value. If that is not possible then the variant is set to an invalid time.

Returns a reference to the stored time.

See also toTime().  

uint & QVariant::asUInt ()

Returns the variant's value as unsigned int reference.  

Q_ULLONG & QVariant::asU