Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
The QVariant class acts like a union for the most common Qt data types. More...
#include <qvariant.h>
Because C++ forbids unions from including types that have non-default constructors or destructors, most interesting Qt classes cannot be used in unions. This is a problem when using QObject::property(), among other things.
This class provides union functionality for property() and most other needs that might be solved by a union including e.g. QWidget.
A QVariant object can hold any one type() at a time. For example, you can find out what type, T, it 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 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 have store QValueList See also Miscellaneous Classes and Object Model.
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 definition of bool depends on the compiler.
qglobal.h has the system-dependent definition of bool.
If you want to modify the QCString after you've passed it to this
constructor, we recommend passing a deep copy (see
QCString::copy()).
If val is null, the resulting variant has type Invalid.
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.
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.
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().
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().
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().
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().
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().
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().
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().
Tries to convert the variant to hold a QColorGroup value. If that
is not possible the variant is set to 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 the variant is set to a default arrow cursor.
Returns a reference to the stored cursor.
See also toCursor().
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().
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().
Tries to convert the variant to hold a QFont. If that
is not possible the variant is set to 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 the variant is set to 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 the variant is set to a null image.
Returns a reference to the stored image.
See also toImage().
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().
Note that if you want to iterate over the list, you should
iterate over a copy, e.g.
Note that if you want to iterate over the list, you should
iterate over a copy, e.g.
Tries to convert the variant to hold a QPalette value. If that
is not possible the variant is set to 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 the variant is set to 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 the variant is set to 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 the variant is set to 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 the variant is set to 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 the variant is set to 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 the variant is set to an invalid size.
Returns a reference to the stored size.
See also toSize() and QSize::isValid().
Tries to convert the variant to hold a QSizePolicy value. If that
fails, the variant is set to an arbitrary size policy.
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().
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.
See also toStringList().
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().
The following casts are done automatically:
See also canCast().
Returns TRUE if the storage type of this variant is not
QVariant::Invalid; otherwise returns FALSE.
Returns an iterator to the first item in the list if the
variant's type is appropriate, or else a null iterator.
Returns the end iterator for the list if the variant's type is
appropriate, or else a null iterator.
Returns an iterator to the first item in the map, if the
variant's type is appropriate, or else a null iterator.
Returns the end iterator for the map, if the variant's type is
appropriate, or else a null iterator.
Returns an iterator to the item in the map with key as key, if the
variant's type is appropriate and key is a valid key, or else a
null iterator.
If the string representation cannot be converted to any enum
representation, the variant is set to Invalid.
This is a deep copy of the variant, but note that if the variant
holds an explicitly shared type such as QImage, a shallow copy
is performed.
Returns an iterator to the first string in the list if the
variant's type is StringList, or else a null iterator.
Returns the end iterator for the list if the variant's type is
StringList, or else a null iterator.
See also asBitArray().
See also asBitmap().
Returns TRUE if the variant has type Int, UInt or Double and its value
is non-zero; otherwise returns FALSE.
See also asBool().
See also asBrush().
See also asByteArray().
See also asCString().
See also asColor().
See also asColorGroup().
See also asCursor().
Note that if the type() is String an invalid date will be returned
if the string cannot be parsed as an Qt::ISODate format date.
See also asDate().
Note that if the type() is String an invalid date/time will be
returned if the string cannot be parsed as an Qt::ISODate format
date/time.
See also asDateTime().
If ok is non-null, *ok is set to TRUE if the value could be
converted to a double and FALSE otherwise.
See also asDouble().
See also asFont().
See also asIconSet().
See also asImage().
If ok is non-null, *ok is set to TRUE if the value could be
converted to an int and FALSE otherwise.
See also asInt() and canCast().
Note that not all Ints and Strings are valid key sequences and in
such cases an empty key sequence will be returned.
See also asKeySequence().
Note that if you want to iterate over the list, you should
iterate over a copy, e.g.
See also asList().
Note that if you want to iterate over the list, you should
iterate over a copy, e.g.
See also asMap().
See also asPalette().
See also asPixmap().
See also asPoint().
See also asPointArray().
See also asRect().
See also asRegion().
See also asSize().
See also asString().
Note that if you want to iterate over the list, you should
iterate over a copy, e.g.
See also asStringList().
Note that if the type() is String an invalid time will be returned
if the string cannot be parsed as an Qt::ISODate format time.
See also asTime().
If ok is non-null, *ok is set to TRUE if the value could be
converted to a uint and FALSE otherwise.
See also asUInt().
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.
This file is part of the Qt toolkit.
Copyright © 1995-2002
Trolltech. All Rights Reserved.Member Type Documentation
QVariant::Type
Member Function Documentation
QVariant::QVariant ()
Constructs an invalid variant.
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.
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.
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.
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.
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 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 ( 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 ()
Destroys the QVariant and the contained object.
QBitArray & QVariant::asBitArray ()
QBitmap & QVariant::asBitmap ()
bool & QVariant::asBool ()
Returns the variant's value as bool reference.
QBrush & QVariant::asBrush ()
QByteArray & QVariant::asByteArray ()
QCString & QVariant::asCString ()
QColor & QVariant::asColor ()
QColorGroup & QVariant::asColorGroup ()
QCursor & QVariant::asCursor ()
QDate & QVariant::asDate ()
QDateTime & QVariant::asDateTime ()
double & QVariant::asDouble ()
Returns the variant's value as double reference.
QFont & QVariant::asFont ()
QIconSet & QVariant::asIconSet ()
QImage & QVariant::asImage ()
int & QVariant::asInt ()
Returns the variant's value as int reference.
QKeySequence & QVariant::asKeySequence ()
QValueList<QVariant> & QVariant::asList ()
Returns the variant's value as variant list reference.
QValueList<QVariant> list = myVariant.asList();
QValueList<QVariant>::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
QMap<QString, QVariant> & QVariant::asMap ()
Returns the variant's value as variant map reference.
QMap<QString, QVariant> list = myVariant.asMap();
QMap<QString, QVariant>::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
QPalette & QVariant::asPalette ()
QPixmap & QVariant::asPixmap ()
QPoint & QVariant::asPoint ()
QPointArray & QVariant::asPointArray ()
QRect & QVariant::asRect ()
QRegion & QVariant::asRegion ()
QSize & QVariant::asSize ()
QSizePolicy & QVariant::asSizePolicy ()
QString & QVariant::asString ()
QStringList & QVariant::asStringList ()
QStringList list = myVariant.asStringList();
QStringList::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
QTime & QVariant::asTime ()
uint & QVariant::asUInt ()
Returns the variant's value as unsigned int reference.
bool QVariant::canCast ( Type t ) const
Returns TRUE if the variant's type can be cast to the requested
type, t. Such casting is done automatically when calling the
toInt(), toBool(), ... or asInt(), asBool(), ... methods.
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 t is
QVariant::String, an empty point array if the requested type t is
QVariant::PointArray, etc). Returns TRUE if the current type of the
variant was successfully casted; otherwise returns FALSE.
void QVariant::clear ()
Convert this variant to type Invalid and free up any resources
used.
bool QVariant::isValid () const
QValueListConstIterator<QVariant> QVariant::listBegin () const
QValueListConstIterator<QVariant> QVariant::listEnd () const
QMapConstIterator<QString, QVariant> QVariant::mapBegin () const
QMapConstIterator<QString, QVariant> QVariant::mapEnd () const
QMapConstIterator<QString, QVariant> QVariant::mapFind ( const QString & key ) const
Type QVariant::nameToType ( const char * name ) [static]
Converts the string representation of the storage type gven in name, to its enum representation.
bool QVariant::operator!= ( const QVariant & v ) const
Compares this QVariant with v and returns TRUE if they are
not equal; otherwise returns FALSE.
QVariant & QVariant::operator= ( const QVariant & variant )
Assigns the value of the variant variant to this variant.
bool QVariant::operator== ( const QVariant & v ) const
Compares this QVariant with v and returns TRUE if they are
equal; otherwise returns FALSE.
QValueListConstIterator<QString> QVariant::stringListBegin () const
QValueListConstIterator<QString> QVariant::stringListEnd () const
const QBitArray QVariant::toBitArray () const
Returns the variant as a QBitArray if the variant has type()
BitArray, or an empty bitarray otherwise.
const QBitmap QVariant::toBitmap () const
Returns the variant as a QBitmap if the variant has type()
Bitmap, or a null QBitmap otherwise.
bool QVariant::toBool () const
Returns the variant as a bool if the variant has type() Bool.
const QBrush QVariant::toBrush () const
Returns the variant as a QBrush if the variant has type()
Brush, or a default brush (with all black colors) otherwise.
const QByteArray QVariant::toByteArray () const
Returns the variant as a QByteArray if the variant has type()
ByteArray, or an empty bytearray otherwise.
const QCString QVariant::toCString () const
Returns the variant as a QCString if the variant has type()
CString or String, or a 0 otherwise.
const QColor QVariant::toColor () const
Returns the variant as a QColor if the variant has type()
Color, or an invalid color otherwise.
const QColorGroup QVariant::toColorGroup () const
Returns the variant as a QColorGroup if the variant has type()
ColorGroup, or a completely black color group otherwise.
const QCursor QVariant::toCursor () const
Returns the variant as a QCursor if the variant has type()
Cursor, or the default arrow cursor otherwise.
const QDate QVariant::toDate () const
Returns the variant as a QDate if the variant has type()
Date, DateTime or String, or an invalid date otherwise.
const QDateTime QVariant::toDateTime () const
Returns the variant as a QDateTime if the variant has type()
DateTime or String, or an invalid date/time otherwise.
double QVariant::toDouble ( bool * ok = 0 ) const
Returns the variant as a double if the variant has type()
String, CString, Double, Int, UInt, or Bool; or 0.0 otherwise.
const QFont QVariant::toFont () const
Returns the variant as a QFont if the variant has type()
Font, or the default font otherwise.
const QIconSet QVariant::toIconSet () const
Returns the variant as a QIconSet if the variant has type()
IconSet, or an icon set of null pixmaps otherwise.
const QImage QVariant::toImage () const
Returns the variant as a QImage if the variant has type()
Image, or a null image otherwise.
int QVariant::toInt ( bool * ok = 0 ) const
Returns the variant as an int if the variant has type()
String, CString, Int, UInt, Double, Bool or KeySequence; or 0 otherwise.
const QKeySequence QVariant::toKeySequence () const
Returns the variant as a QKeySequence if the variant has type()
KeySequence, Int or String, or an empty key sequence otherwise.
const QValueList<QVariant> QVariant::toList () const
Returns the variant as a QValueList
QValueList<QVariant> list = myVariant.toList();
QValueList<QVariant>::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
const QMap<QString, QVariant> QVariant::toMap () const
Returns the variant as a QMap
QMap<QString, QVariant> list = myVariant.toMap();
QMap<QString, QVariant>::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
const QPalette QVariant::toPalette () const
Returns the variant as a QPalette if the variant has type()
Palette, or a completely black palette otherwise.
const QPixmap QVariant::toPixmap () const
Returns the variant as a QPixmap if the variant has type()
Pixmap, or a null pixmap otherwise.
const QPoint QVariant::toPoint () const
Returns the variant as a QPoint if the variant has type()
Point, or a point (0, 0) otherwise.
const QPointArray QVariant::toPointArray () const
Returns the variant as a QPointArray if the variant has type()
PointArray, or an empty QPointArray otherwise.
const QRect QVariant::toRect () const
Returns the variant as a QRect if the variant has type()
Rect, or an empty rectangle otherwise.
const QRegion QVariant::toRegion () const
Returns the variant as a QRegion if the variant has type()
Region, or an empty QRegion otherwise.
const QSize QVariant::toSize () const
Returns the variant as a QSize if the variant has type()
Size, or an invalid size otherwise.
QSizePolicy QVariant::toSizePolicy () const
Returns the variant as a QSizePolicy if the variant has type()
SizePolicy, or an undefined (but legal) size policy otherwise.
const QString QVariant::toString () const
Returns the variant as a QString if the variant has type()
String, CString, ByteArray, Int, Uint, Bool, Double, Date, Time, or DateTime,
or QString::null otherwise.
const QStringList QVariant::toStringList () const
Returns the variant as a QStringList if the variant has type()
StringList or List of a type that can be converted to QString, or an
empty list otherwise.
QStringList list = myVariant.toStringList();
QStringList::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
const QTime QVariant::toTime () const
Returns the variant as a QTime if the variant has type()
Time, DateTime or String, or an invalid time otherwise.
uint QVariant::toUInt ( bool * ok = 0 ) const
Returns the variant as an unsigned int if the variant has type()
String, CString, UInt, Int, Double, or Bool; or 0 otherwise.
Type QVariant::type () const
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 "QValueListconst char * QVariant::typeToName ( Type typ ) [static]
Converts the enum representation of the storage type, typ, to its
string representation.
Copyright © 2002
Trolltech Trademarks