Qt logo

QVariant Class Reference


Acts like a union for the most common Qt data types. More...

#include <qvariant.h>

Inherits QShared.

List of all member functions.

Public Members

Static Public Members

Protected Members


Detailed Description

Acts like a union for the most common Qt data types.

C++ forbids unions from including classes that have constructors and destructors since the compiler and the runtime library cannot not determine which constructor or destructor to call when an object goes in and out of scope.

To overcome this, a QVariant can be used to store the most common Qt and C++ data types. Like a union it can hold only one value of one type at any one time.

For each QVariant::Type that the variant can hold, there is a constructor to create a QVariant from a value of the type, a setValue(T) function to change a variant to a value of the type, and a toT() function to retrieve the value. For example:

    QDataStream out(...);
    QVariant v(123);          // The variant now contains an int
    int x = v.toInt();        // x = 123
    out << v;                 // Writes a type tag an int to out
    v.setValue("hello");      // The variant now contains a QCString
    v.setValue(tr("hello"));  // The variant now contains a QString
    int y = v.toInt();        // x = 0, since v is not 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());

You can even have a QValueList stored in the variant - giving arbitrarily complex data values with lists of variants, some of which are strings while others are integers and other still are lists of lists of lists of variants. Of course, you'll need to be careful with your encapsulations or else the typelessness will make your code look like spam, spam, spam, baked beans and spam.

You can find the type of a variant with type(). There is a special type, Invalid, which can be used for special cases. The isValid() function tests for this type.

An exception to this pattern is setBoolValue(), which cannot be called setValue(bool), since some compilers cannot distinguish between bool and int.


Member Type Documentation

QVariant::Type

This enum type defines the types of variable that a QVariant can contain. The supported enum values and the associated types are: Note that Qt's idea 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 ( QDataStream & s )

Reads the variant from the data stream.

QVariant::QVariant ( bool val )

Constructs a new variant with a boolean value.

QVariant::QVariant ( const QBrush & val )

Constructs a new variant with a brush value.

QVariant::QVariant ( const QColor & val )

Constructs a new variant with a color value.

QVariant::QVariant ( const QColorGroup & val )

Constructs a new variant with a color group value.

QVariant::QVariant ( const QCString & val )

Constructs a new variant with a c-string value.

QVariant::QVariant ( const QFont & val )

Constructs a new variant with a font value.

QVariant::QVariant ( const QIconSet & val )

Constructs a new variant with an empty iconset.

QVariant::QVariant ( const QImage & val )

Constructs a new variant with an image value.

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

Constructs a new variant with a map of QVariants.

QVariant::QVariant ( const QPalette & val )

Constructs a new variant with a color palette value.

QVariant::QVariant ( const QPixmap & val )

Constructs a new variant with a pixmap value.

QVariant::QVariant ( const QPoint & val )

Constructs a new variant with a point value.

QVariant::QVariant ( const QRect & val )

Constructs a new variant with a rect value.

QVariant::QVariant ( const QSize & val )

Constructs a new variant with a size value.

QVariant::QVariant ( const QString & val )

Constructs a new variant with a string value.

QVariant::QVariant ( const QStringList & val )

Constructs a new variant with a string list value.

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

Constructs a new variant with a list value.

QVariant::QVariant ( const QVariant & p )

Constructs a deep copy of the variant passed as argument to this constructor.

QVariant::QVariant ( const char * val )

Constructs a new variant with a c-string value.

QVariant::QVariant ( double val )

Constructs a new variant with a floating point value.

QVariant::QVariant ( int val )

Constructs a new variant with an integer value.

QVariant::~QVariant () [virtual]

Destructs the QVariant and the contained object.

Note that subclasses that re-implement clear() should reimplement the destructor to call clear(). This destructor calls clear(), but since it is the destructor, QVariant::clear() is called rather than any subclass.

bool QVariant::canCast ( Type t ) const

Returns TRUE if the current type of the variant can be casted to the requested type. The casting is done automatically when calling the toInt(), toBool(), ... methods.

The following casts are done automatically:

void QVariant::clear () [virtual protected]

De-allocate any used memory, based on the type, producing an Invalid variant.

bool QVariant::isValid () const

Returns TRUE if the storage type of this variant is not QVariant::Invalid.

void QVariant::load ( QDataStream & s ) [virtual]

Internal function for loading a variant. Use the stream operators instead.

QVariant::Type QVariant::nameToType( const char * name ) [static]

Converts the string representation of the storage type to its enum representation.

QVariant& QVariant::operator= ( const QVariant & other )

Assigns the value of some other variant to this variant. This is a deep copy.

void QVariant::save ( QDataStream & s ) const [virtual]

Internal function for saving a variant. Use the stream operators instead.

void QVariant::setBoolValue ( bool val )

Changes the value of this variant to val. This is not called setValue(bool), since some compilers cannot distinguish between bool and int.

void QVariant::setValue ( const QBrush & val )

Changes the value of this variant to val.

void QVariant::setValue ( const QColor & val )

Changes the value of this variant to val.

void QVariant::setValue ( const QColorGroup & val )

Changes the value of this variant to val.

void QVariant::setValue ( const QCString & val )

Changes the value of this variant to val.

void QVariant::setValue ( const QFont & val )

Changes the value of this variant to val.

void QVariant::setValue ( const QIconSet & val )

Changes the value of this variant to val.

void QVariant::setValue ( const QImage & val )

Changes the value of this variant to val.

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

Changes the value of this variant to val. This function creates a copy of the map. This is very fast since QMap is implicit shared.

void QVariant::setValue ( const QPalette & val )

Changes the value of this variant to val.

void QVariant::setValue ( const QPixmap & val )

Changes the value of this variant to val.

void QVariant::setValue ( const QPoint & val )

Changes the value of this variant to val.

void QVariant::setValue ( const QRect & val )

Changes the value of this variant to val.

void QVariant::setValue ( const QSize & val )

Changes the value of this variant to val.

void QVariant::setValue ( const QString & val )

Changes the value of this variant to val.

void QVariant::setValue ( const QStringList & val )

Changes the value of this variant to val. This function creates a copy of the list. This is very fast since QStringList is implicit shared.

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

Changes the value of this variant to val. This function creates a copy of the list.

void QVariant::setValue ( const char * val )

Changes the value of this variant to val. The Variant creates a copy of the passed string.

void QVariant::setValue ( double val )

Changes the value of this variant to val.

void QVariant::setValue ( int val )

Changes the value of this variant to val.

bool QVariant::toBool () const

Returns the variant as a bool if the variant has type() Bool, or FALSE otherwise. The only exceptions to this rule are the types Int, Double. In this case TRUE is returned if the numerical value is not zero or FALSE otherwise.

QBrush QVariant::toBrush () const

Returns the variant as a QBrush if the variant has type() Brush, or a default brush otherwise.

QCString QVariant::toCString () const

Returns the variant as a QCString if the variant has type() CString, or a null QCString otherwise.

QColor QVariant::toColor () const

Returns the variant as a QColor if the variant has type() Color, or an invalid color otherwise.

QColorGroup QVariant::toColorGroup () const

Returns the variant as a QColorGroup if the variant has type() ColorGroup, or a completely black color group otherwise.

double QVariant::toDouble () const

Returns the variant as a double if the variant has type() Double, Int or Bool, or 0.0 otherwise.

QFont QVariant::toFont () const

Returns the variant as a QFont if the variant has type() Font, or the default font otherwise.

QIconSet QVariant::toIconSet () const

Returns the variant as a QIconSet if the variant has type() IconSet, or an icon set of null pixmaps otherwise.

QImage QVariant::toImage () const

Returns the variant as a QImage if the variant has type() Image, or a null image otherwise.

int QVariant::toInt () const

Returns the variant as an int if the variant has type() Int, Double or Bool, or 0 otherwise.

QValueList<QVariant> QVariant::toList () const

Returns the variant as a QValueList if the variant has type() List or StringList, or an empty list otherwise.

QPalette QVariant::toPalette () const

Returns the variant as a QPalette if the variant has type() Palette, or a completely black palette otherwise.

QPixmap QVariant::toPixmap () const

Returns the variant as a QPixmap if the variant has type() Pixmap, or a null pixmap otherwise.

QPoint QVariant::toPoint () const

Returns the variant as a QPoint if the variant has type() Point, or a the point (0,0) otherwise.

QRect QVariant::toRect () const

Returns the variant as a QRect if the variant has type() Rect, or an empty rectangle otherwise.

QSize QVariant::toSize () const

Returns the variant as a QSize if the variant has type() Size, or an invalid size otherwise.

QString QVariant::toString () const

Returns the variant as a QString if the variant has type() String or CString, or QString::null otherwise.

QStringList QVariant::toStringList () const

Returns the variant as a QStringList if the variant has type() StringList or List, or an empty list otherwise.

Type QVariant::type () const

Returns the stoarge type of the value stored in the variant currently.

const char* QVariant::typeName () const [virtual]

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". An Invalid variant returns 0.

const char* QVariant::typeToName ( Type typ ) [static]

Converts the enum representation of the storage type to its string representation.


Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):


This file is part of the Qt toolkit, copyright © 1995-2000 Troll Tech, all rights reserved.


Copyright İ 2000 Troll TechTrademarks
Qt version 2.1.0 (pre-release)