Acts like a union for the most common Qt data types. More...
#include <qvariant.h>
C++ forbids unions from including classes that have constructors and destructors since the compiler and the runtime library cannot 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, and two access methods to retrieve the value. The access methods do automatic conversion. Which conversion is possible can be queried by calling canCast().
The methods named toT() are const. They return a copy of the stored data type or do conversion if needed. The conversion is only done for the returned value but the variant itself does not change. Please notice that some Qt data types such as QImage, QBrush, QPointArray, QRegion, QCString and QPalette are explicit shared. The toT() methods only return a shallow copy. That means you should make a deep copy of the returned values before modifying them.
The methods named asT() are not const. They act like the toT() methods except for two very important points. asT() returns a reference to the stored value instead of returning a copy and the casting affects the variant itself. That means if you store for example an interger in your variant and call asDouble() then the variant is converted to contain a double now and a reference to this double is returned.
Here is some example code to demonstrate the usage 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 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(); // 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()); v.asInt() += 100; // The variant now hold the value 223. v = QVariant( QStringList() ); v.asStringList().append( "Hallo" );
You can even have a QValueList 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.
Constructs an invalid variant.
Reads the variant from the data stream.
Constructs a new variant with a boolean value. The integer argument
is a dummy, so even the (name and description omitted) compiler can
handle QVariant.
Constructs a new variant with a bitmap.
Constructs a new variant with a brush value. The variant creates only
a shallow copy of the brush since QBrush is explicit shared.
Constructs a new variant with a color value.
Constructs a new variant with a color group value.
Constructs a new variant with a c-string value. If you want to
modify the QCString you passed to this constructor afterwards,
then pass a deep copy by calling QCString::copy().
Constructs a new variant with a cursor.
Constructs a new variant with a font value.
Constructs a new variant with an iconset.
Constructs a new variant with an image value. Since QImage is
explicit shared you may consider to pass a deep copy to
the variant by calling QImage::copy().
Constructs a new variant with a map of QVariants.
Constructs a new variant with a color palette value. Since QPalette
is explicit shared you may consider to pass a deep copy to the
variant by calling QPalette::copy().
Constructs a new variant with a pixmap value.
Constructs a new variant with a point value.
Constructs a new variant with an array of points. Since QPointArray
is explicit shared you may consider to pass a deep copy to the variant
by calling QPointArray::copy().
Constructs a new variant with a rect value.
Constructs a new variant with a region. The variant creates only
a shallow copy of the brush since QBrush is explicit shared.
Constructs a new variant with a size value.
Constructs a new variant with a string value.
Constructs a new variant with a string list value.
Constructs a new variant with a list value.
Constructs a copy of the variant passed as argument to this
constructor. Usually this is a deep copy. But if the stored data
type is explicit shared then only a shallow copy is made.
The variant just resembles the copy behaviour of the data type
it contains.
Constructs a new variant with a c-string value. The variant
creates a deep copy of the string. Passing a null string will
result in an invalid variant.
Constructs a new variant with a floating point value.
Constructs a new variant with an integer value.
Constructs a new variant with an unsigned integer value.
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.
Tries to convert the variant to hold a bitmap value. If that
is not possible then the variant holds a null bitmap.
Returns a reference to the stored bitmap.
See also: toBitmap().
Returns the variant's value as bool reference.
Tries to convert the variant to hold a brush value. If that
is not possible then the variant holds a default black brush.
Returns a reference to the stored brush.
See also: toBrush().
Tries to convert the variant to hold a string value. If that
is not possible then the variant holds an empty string.
Returns a reference to the stored string.
See also: toCString().
Tries to convert the variant to hold a QColor value. If that
is not possible then the variant holds an invalid color.
Returns a reference to the stored color.
See also: toColor() and QColor::isValid().
Tries to convert the variant to hold a QColorGroup value. If that
is not possible then the variant holds a color group with all colors
set to black.
Returns a reference to the stored color group.
See also: toColorGroup().
Tries to convert the variant to hold a QCursor value. If that
is not possible then the variant holds a default arrow cursor.
Returns a reference to the stored cursor.
See also: toCursor().
Returns the variant's value as double reference.
Tries to convert the variant to hold a QFont. If that
is not possible then the variant holds a default font.
Returns a reference to the stored font.
See also: toFont().
Tries to convert the variant to hold a QIconSet value. If that
is not possible then the variant holds an empty iconset.
Returns a reference to the stored iconset.
See also: toIconSet().
Tries to convert the variant to hold an image value. If that
is not possible then the variant holds a null image.
Returns a reference to the stored image.
See also: toImage().
Returns the variant's value as int reference.
Returns the variant's value as variant list reference.
Tries to convert the variant to hold a QPalette value. If that
is not possible then the variant holds a palette with black colors only.
Returns a reference to the stored palette.
See also: toString().
Tries to convert the variant to hold a pixmap value. If that
is not possible then the variant holds a null pixmap.
Returns a reference to the stored pixmap.
See also: toPixmap().
Tries to convert the variant to hold a point value. If that
is not possible then the variant holds a null point.
Returns a reference to the stored point.
See also: toPoint().
Tries to convert the variant to hold a QPointArray value. If that
is not possible then the variant holds an empty point array.
Returns a reference to the stored point array.
See also: toPointArray().
Tries to convert the variant to hold a rectangle value. If that
is not possible then the variant holds an empty rectangle.
Returns a reference to the stored rectangle.
See also: toRect().
Tries to convert the variant to hold a QRegion value. If that
is not possible then the variant holds a null region.
Returns a reference to the stored region.
See also: toRegion().
Tries to convert the variant to hold a QSize value. If that
is not possible then the variant holds an invalid size.
Returns a reference to the stored size.
See also: toSize() and QSize::isValid().
Tries to convert the variant to hold a string value. If that
is not possible then the variant holds an empty string.
Returns a reference to the stored string.
See also: toString().
Tries to convert the variant to hold a QStringList value. If that
is not possible then the variant holds an empty string list.
Returns a reference to the stored string list.
See also: toStringList().
Returns the variant's value as unsigned int reference.
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(), ... or asInt(), asBool(), ... methods.
The following casts are done automatically:
De-allocate any used memory,
based on the type, producing an Invalid variant.
Returns TRUE if the storage type of this variant is not QVariant::Invalid.
If the type of the variant is List, then an iterator to the first element
in the list is returned. Otherwise a null iterator is returned.
If the type of the variant is List, then the end iterator
of the list is returned. Otherwise a null iterator is returned.
Internal function for loading a variant. Use the stream operators
instead.
Converts the string representation of the storage type to
its enum representation.
Compares this QVariant with v and returns TRUE if they are
not equal, FALSE othervise.
Assigns the value of some other variant to this variant.
This is a deep copy.
Compares this QVariant with v and returns TRUE if they are
equal, FALSE othervise.
Internal function for saving a variant. Use the stream operators
instead.
If the type of the variant is StringList, then an iterator to the first string
in the list is returned. Otherwise a null iterator is returned.
If the type of the variant is StringList, then the end iterator
of the list is returned. Otherwise a null iterator is returned.
Returns the variant as a QBitmap if the variant has type()
Bitmap, or a null QBitmap otherwise.
See also: asBitmap().
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, UInt, Double. In this case TRUE is returned if the numerical
value is not zero or FALSE otherwise.
See also: asBool().
Returns the variant as a QBrush if the variant has type()
Brush, or a default brush with black colors otherwise.
See also: asBrush().
Returns the variant as a QCString if the variant has type()
CString, or a 0 otherwise.
See also: asCString().
Returns the variant as a QColor if the variant has type()
Color, or an invalid color otherwise.
See also: asColor().
Returns the variant as a QColorGroup if the variant has type()
ColorGroup, or a completely black color group otherwise.
See also: asColorGroup().
Returns the variant as a QCursor if the variant has type()
Cursor, or the default arrow cursor otherwise.
See also: asCursor().
Returns the variant as a double if the variant has type()
Double, Int, UInt or Bool, or 0.0 otherwise.
See also: asDouble().
Returns the variant as a QFont if the variant has type()
Font, or the default font otherwise.
See also: asFont().
Returns the variant as a QIconSet if the variant has type()
IconSet, or an icon set of null pixmaps otherwise.
See also: asIconSet().
Returns the variant as a QImage if the variant has type()
Image, or a null image otherwise.
See also: asImage().
Returns the variant as an int if the variant has type()
Int, UInt, Double or Bool, or 0 otherwise.
See also: asInt().
Returns the variant as a QValueList See also: asList().
Returns the variant as a QPalette if the variant has type()
Palette, or a completely black palette otherwise.
See also: asPalette().
Returns the variant as a QPixmap if the variant has type()
Pixmap, or a null pixmap otherwise.
See also: asPixmap().
Returns the variant as a QPoint if the variant has type()
Point, or a the point (0,0) otherwise.
See also: asPoint().
Returns the variant as a QPointArray if the variant has type()
PointArray, or an empty QPointArray otherwise.
See also: asPointArray().
Returns the variant as a QRect if the variant has type()
Rect, or an empty rectangle otherwise.
See also: asRect().
Returns the variant as a QRegion if the variant has type()
Region, or an empty QRegion otherwise.
See also: asRegion().
Returns the variant as a QSize if the variant has type()
Size, or an invalid size otherwise.
See also: asSize().
Returns the variant as a QString if the variant has type()
String or CString, or QString::null otherwise.
See also: asString().
Returns the variant as a QStringList if the variant has type()
StringList or List, or an empty list otherwise.
See also: asStringList().
Returns the variant as an unsigned int if the variant has type()
UInt, Int, Double or Bool, or 0 otherwise.
See also: asUInt().
Returns the stoarge type of the value stored in the
variant currently. Usually you may want to test with
canCast() wether 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", "QString" or "QValueList Converts the enum representation of the storage type to its
string representation.
Search the documentation, FAQ, qt-interest archive and more (uses
www.trolltech.com):
This file is part of the Qt toolkit,
copyright © 1995-2000
Troll Tech, all rights reserved.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.
Invalid
- no type
List
- a QValueListMap
- a QMapString
- a QString
StringList
- a QStringList
Font
- a QFont
Pixmap
- a QPixmap
Brush
- a QBrush
Rect
- a QRect
Size
- a QSize
Color
- a QColor
Palette
- a QPalette
ColorGroup
- a QColorGroup
IconSet
- a QIconSet
Point
- a QPoint
Image
- a QImage
Int
- an int
UInt
- an unsigned int
Bool
- a bool
Double
- a doublea
CString
- a QCString
PointArray
- a QPointArray
Region
- a QRegion
Bitmap
- a QBitmap
Cursor
- a QCursor
Member Function Documentation
QVariant::QVariant ()
QVariant::QVariant ( QDataStream & s )
QVariant::QVariant ( bool val, int )
QVariant::QVariant ( const QBitmap & val )
QVariant::QVariant ( const QBrush & val )
QVariant::QVariant ( const QColor & val )
QVariant::QVariant ( const QColorGroup & val )
QVariant::QVariant ( const QCString & val )
QVariant::QVariant ( const QCursor & val )
QVariant::QVariant ( const QFont & val )
QVariant::QVariant ( const QIconSet & val )
QVariant::QVariant ( const QImage & val )
QVariant::QVariant ( const QMap<QString,QVariant> & val )
QVariant::QVariant ( const QPalette & val )
QVariant::QVariant ( const QPixmap & val )
QVariant::QVariant ( const QPoint & val )
QVariant::QVariant ( const QPointArray & val )
QVariant::QVariant ( const QRect & val )
QVariant::QVariant ( const QRegion & val )
QVariant::QVariant ( const QSize & val )
QVariant::QVariant ( const QString & val )
QVariant::QVariant ( const QStringList & val )
QVariant::QVariant ( const QValueList<QVariant> & val )
QVariant::QVariant ( const QVariant & p )
QVariant::QVariant ( const char * val )
QVariant::QVariant ( double val )
QVariant::QVariant ( int val )
QVariant::QVariant ( uint val )
QVariant::~QVariant ()
QBitmap& QVariant::asBitmap ()
bool& QVariant::asBool ()
QBrush& QVariant::asBrush ()
QCString& QVariant::asCString ()
QColor& QVariant::asColor ()
QColorGroup& QVariant::asColorGroup ()
QCursor& QVariant::asCursor ()
double& QVariant::asDouble ()
QFont& QVariant::asFont ()
QIconSet& QVariant::asIconSet ()
QImage& QVariant::asImage ()
int& QVariant::asInt ()
QValueList<QVariant>& QVariant::asList ()
QPalette& QVariant::asPalette ()
QPixmap& QVariant::asPixmap ()
QPoint& QVariant::asPoint ()
QPointArray& QVariant::asPointArray ()
QRect& QVariant::asRect ()
QRegion& QVariant::asRegion ()
QSize& QVariant::asSize ()
QString& QVariant::asString ()
QStringList& QVariant::asStringList ()
uint& QVariant::asUInt ()
bool QVariant::canCast ( Type t ) const
void QVariant::clear ()
bool QVariant::isValid () const
QValueListConstIterator<QVariant> QVariant::listBegin () const
QValueListConstIterator<QVariant> QVariant::listEnd () const
void QVariant::load ( QDataStream & s )
QVariant::Type QVariant::nameToType( const char * name )
[static]
bool QVariant::operator!= ( const QVariant & v ) const
QVariant& QVariant::operator= ( const QVariant & variant )
bool QVariant::operator== ( const QVariant & v ) const
void QVariant::save ( QDataStream & s ) const
QValueListConstIterator<QString> QVariant::stringListBegin () const
QValueListConstIterator<QString> QVariant::stringListEnd () const
const QBitmap QVariant::toBitmap () const
bool QVariant::toBool () const
const QBrush QVariant::toBrush () const
const QCString QVariant::toCString () const
const QColor QVariant::toColor () const
const QColorGroup QVariant::toColorGroup () const
const QCursor QVariant::toCursor () const
double QVariant::toDouble () const
const QFont QVariant::toFont () const
const QIconSet QVariant::toIconSet () const
const QImage QVariant::toImage () const
int QVariant::toInt () const
const QValueList<QVariant> QVariant::toList () const
const QPalette QVariant::toPalette () const
const QPixmap QVariant::toPixmap () const
const QPoint QVariant::toPoint () const
const QPointArray QVariant::toPointArray () const
const QRect QVariant::toRect () const
const QRegion QVariant::toRegion () const
const QSize QVariant::toSize () const
const QString QVariant::toString () const
const QStringList QVariant::toStringList () const
uint QVariant::toUInt () const
Type QVariant::type () const
const char* QVariant::typeName () const
const char* QVariant::typeToName ( Type typ )
[static]
Copyright İ 2000 Troll Tech Trademarks