QVariant - Acts like a union for the most common Qt data
types
#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 }
QVariant ()
~QVariant ()
QVariant ( const QVariant & )
QVariant ( QDataStream & s )
QVariant ( const QString & )
QVariant ( const QCString & )
QVariant ( const char * )
QVariant ( const QStringList & )
QVariant ( const QFont & )
QVariant ( const QPixmap & )
QVariant ( const QImage & )
QVariant ( const QBrush & )
QVariant ( const QPoint & )
QVariant ( const QRect & )
QVariant ( const QSize & )
QVariant ( const QColor & )
QVariant ( const QPalette & )
QVariant ( const QColorGroup & )
QVariant ( const QIconSet & )
QVariant ( const QPointArray & )
QVariant ( const QRegion & )
QVariant ( const QBitmap & )
QVariant ( const QCursor & )
QVariant ( const QValueList<QVariant> & )
QVariant ( const QMap<QString, QVariant> & )
QVariant ( int )
QVariant ( uint )
QVariant ( bool, int )
QVariant ( double )
QVariant ( QSizePolicy )
QVariant& operator= ( const QVariant & )
bool operator== ( const QVariant & ) const
bool operator!= ( const QVariant & ) const
Type type () const
const char* typeName () const
bool canCast ( Type ) const
bool isValid () const
void clear ()
const QString toString () const
const QCString toCString () const
const QStringList toStringList () 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
int toInt () const
uint toUInt () const
bool toBool () const
double toDouble () const
const QValueList<QVariant> toList () const
const QMap<QString, QVariant> toMap () const
QSizePolicy toSizePolicy () const
QValueListConstIterator<QVariant> listBegin () const
QValueListConstIterator<QVariant> listEnd () const
QValueListConstIterator<QString> stringListBegin () const
QValueListConstIterator<QString> stringListEnd () const
QMapConstIterator<QString, QVariant> mapBegin () const
QMapConstIterator<QString, QVariant> mapEnd () const
QMapConstIterator<QString, QVariant> mapFind ( const
QString & ) const
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 ()
int& asInt ()
uint& asUInt ()
bool& asBool ()
double& asDouble ()
QValueList<QVariant>& asList ()
QMap<QString, QVariant>& asMap ()
void load ( QDataStream & )
void save ( QDataStream & ) const
Static Public Members
const char* typeToName ( Type typ )
Type nameToType ( const char * name )
DESCRIPTION
Acts like a union for the most common Qt data types.
Since 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, and
you can find out what type it holds, convert it to a
different type using e.g. asSize(), get its value using
e.g. toSize(), and check whether the type can be converted
to e.g. QSize 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 which 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 methods named asT() are not const. They do conversion
like 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 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
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<QVariant> 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. This is very powerful, and you can easily
shoot yourself in the foot with all this power. Caveat
programmor.
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:
Invalid - no type
List - a QValueList<QVariant>
Map - a QMap<QString,QVariant>
String - 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
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
SizePolicy - a QSizePolicy
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 ( QDataStream & s )
Reads the variant from the data stream.
QVariant::QVariant ( QSizePolicy val )
Constructs a new variant with a size policy value.
QVariant::QVariant ( bool val, int )
Constructs a new variant with a boolean value. The integer
argument is a dummy, necessary for compatibility with
certain compiler that even its mother cannot love.
QVariant::QVariant ( const QBitmap & val )
Constructs a new variant with a bitmap 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.
Constructs a new variant with a color group value.
QVariant::QVariant ( const QCString & val )
Constructs a new variant with a c-string value.
If you want to modify the QCString you pass to this
constructor after this call, we recommend passing a deep
copy (see QCString::copy()).
QVariant::QVariant ( const QCursor & val )
Constructs a new variant with a cursor 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 icon set value.
QVariant::QVariant ( const QImage & val )
Constructs a new variant with an image value.
Since QImage is explicitly shared you may need to pass a
deep copy to the variant using QImage::copy().
QVariant::QVariant ( const QMap & 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 QPointArray & val )
Constructs a new variant with an point array value.
Since QPointArray is explicitly shared you may need to
pass a deep copy to the variant using QPointArray::copy().
QVariant::QVariant ( const QRect & val )
Constructs a new variant with a rect value.
QVariant::QVariant ( const QRegion & val )
Constructs a new variant with a region.
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.
Constructs a new variant with a string list value.
QVariant::QVariant ( const QValueList & val )
Constructs a new variant with a list value.
QVariant::QVariant ( const QVariant & p )
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 a shallow copy is
made.
QVariant::QVariant ( const char * val )
Constructs a new variant with a c-string value, 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 ( 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 ( uint val )
Constructs a new variant with an unsigned integer value.
QVariant::~QVariant ()
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.
QBitmap& QVariant::asBitmap ()
Tries to convert the variant to hold a bitmap value. If
that is not possible then 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 then the variant is set to a default
black brush.
Returns a reference to the stored brush.
QCString& QVariant::asCString ()
Tries to convert the variant to hold a string value. If
that is not possible then 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 then 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 then 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().
QCursor& QVariant::asCursor ()
Tries to convert the variant to hold a QCursor value. If
that is not possible then the variant is set to a default
arrow cursor.
Returns a reference to the stored cursor.
See also toCursor().
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 then the variant is set to a 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 then the variant is set to an empty
iconset.
See also toIconSet().
QImage& QVariant::asImage ()
Tries to convert the variant to hold an image value. If
that is not possible then 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.
QValueList& QVariant::asList ()
Returns the variant's value as variant list reference.
QMap& QVariant::asMap ()
Returns the variant's value as variant map reference.
QPalette& QVariant::asPalette ()
Tries to convert the variant to hold a QPalette value. If
that is not possible then the variant is set to a palette
with black colors only.
Returns a reference to the stored palette.
See also toString().
QPixmap& QVariant::asPixmap ()
Tries to convert the variant to hold a pixmap value. If
that is not possible then 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 then the variant is set to a null
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 then the variant is set to an
empty point array.
See also toPointArray().
QRect& QVariant::asRect ()
Tries to convert the variant to hold a rectangle value. If
that is not possible then 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 then 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 then 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 size
policy.
QString& QVariant::asString ()
Tries to convert the variant to hold a string value. If
that is not possible then 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 then the variant is set to an
empty string list.
Returns a reference to the stored string list.
See also toStringList().
Returns the variant's value as unsigned int reference.
bool QVariant::canCast ( Type t ) const
Returns TRUE if the current type of the variant can be
cast to the requested type. Such casting is done
automatically when calling the toInt(), toBool(), ... or
asInt(), asBool(), ... methods.
The following casts are done automatically:
Bool -> Double, Int, UInt
Double -> Int, Bool, UInt
Int -> Double, Bool, UInt
UInt -> Double, Bool, Int
String -> CString
CString -> String
List -> StringList (if the list contains strings or
something that can be cast to a string).
StringList -> List
void QVariant::clear ()
Convert this variant to type Invalid and free up any
resources used.
bool QVariant::isValid () const
Returns TRUE if the storage type of this variant is not
QVariant::Invalid.
QValueListConstIterator QVariant::listBegin () const
Returns an iterator to the first item in the list, if the
variant's type is appropriate, or else a null iterator.
QValueListConstIterator QVariant::listEnd () const
Returns the end iterator for the list, if the variant's
type is appropriate, or else a null iterator.
void QVariant::load ( QDataStream & s )
Internal function for loading a variant. Use the stream
operators instead.
QMapConstIterator QVariant::mapBegin () const
Returns an iterator to the first item in the map, if the
variant's type is appropriate, or else a null iterator.
QMapConstIterator QVariant::mapEnd () const
Returns the end iterator for the map, if the variant's
QMapConstIterator QVariant::mapFind ( const
QString & key ) const
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.
QVariant::Type QVariant::nameToType( const char * name ) [static]
Converts the string representation of the storage type to
its enum representation.
If the string representation cannot be converted to any
enum representation, the variant is set to Invalid.
bool QVariant::operator!= ( const QVariant & v ) const
Compares this QVariant with v and returns TRUE if they are
not equal, FALSE otherwise.
QVariant& QVariant::operator= ( const QVariant & variant )
Assigns the value of some other variant to this variant.
This is a deep copy of the variant, but note that if the
variant holds an explicitly shared type such as QImage, it
is a shallow copy of the (e.g.) QImage.
bool QVariant::operator== ( const QVariant & v ) const
Compares this QVariant with v and returns TRUE if they are
equal, FALSE otherwise.
void QVariant::save ( QDataStream & s ) const
Internal function for saving a variant. Use the stream
operators instead.
QValueListConstIterator QVariant::stringListBegin ()
const
Returns an iterator to the first string in the list, if
the variant's type is StringList, or else a null iterator.
QValueListConstIterator QVariant::stringListEnd () const
Returns the end iterator for the list, if the variant's
type is StringList, or else a null iterator.
const QBitmap QVariant::toBitmap () const
Returns the variant as a QBitmap if the variant has type()
Bitmap, or a null QBitmap otherwise.
See also asBitmap().
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, UInt, Double. In this case TRUE is
returned if the numerical value is not zero or FALSE
See also asBool().
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.
See also asBrush().
const QCString QVariant::toCString () const
Returns the variant as a QCString if the variant has
type() CString, or a 0 otherwise.
See also asCString().
const QColor QVariant::toColor () const
Returns the variant as a QColor if the variant has type()
Color, or an invalid color otherwise.
See also asColor().
const QColorGroup QVariant::toColorGroup () const
Returns the variant as a QColorGroup if the variant has
type() ColorGroup, or a completely black color group
otherwise.
See also asColorGroup().
const QCursor QVariant::toCursor () const
Returns the variant as a QCursor if the variant has type()
Cursor, or the default arrow cursor otherwise.
See also asCursor().
double QVariant::toDouble () const
Returns the variant as a double if the variant has type()
Double, Int, UInt or Bool, or 0.0 otherwise.
See also asDouble().
const QFont QVariant::toFont () const
Returns the variant as a QFont if the variant has type()
Font, or the default font otherwise.
See also asFont().
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.
See also asIconSet().
Returns the variant as a QImage if the variant has type()
Image, or a null image otherwise.
See also asImage().
int QVariant::toInt () const
Returns the variant as an int if the variant has type()
Int, UInt, Double or Bool, or 0 otherwise.
See also asInt().
const QValueList QVariant::toList () const
Returns the variant as a QValueList<QVariant> if the
variant has type() List or StringList, or an empty list
otherwise.
See also asList().
const QMap QVariant::toMap () const
Returns the variant as a QMap<QString,QVariant> if the
variant has type() Map, or an empty map otherwise.
See also asMap().
const QPalette QVariant::toPalette () const
Returns the variant as a QPalette if the variant has
type() Palette, or a completely black palette otherwise.
See also asPalette().
const QPixmap QVariant::toPixmap () const
Returns the variant as a QPixmap if the variant has type()
Pixmap, or a null pixmap otherwise.
See also asPixmap().
const QPoint QVariant::toPoint () const
Returns the variant as a QPoint if the variant has type()
Point, or a the point (0,0) otherwise.
See also asPoint().
const QPointArray QVariant::toPointArray () const
Returns the variant as a QPointArray if the variant has
type() PointArray, or an empty QPointArray otherwise.
See also asPointArray().
const QRect QVariant::toRect () const
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().
const QSize QVariant::toSize () const
Returns the variant as a QSize if the variant has type()
Size, or an invalid size otherwise.
See also asSize().
QSizePolicy QVariant::toSizePolicy () const
Returns the variant as a QSizePolicy if the variant has
type() SizePolicy, or an undefined but legal size policy
else.
const QString QVariant::toString () const
Returns the variant as a QString if the variant has type()
String or CString, or QString::null otherwise.
See also asString().
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.
See also asStringList().
uint QVariant::toUInt () const
Returns the variant as an unsigned int if the variant has
type() UInt, Int, Double or Bool, or 0 otherwise.
See also asUInt().
Type QVariant::type () const
Returns the storage type of the value stored in the
variant. Usually it's best to test with canCast() wether
the variant can deliver the data type you are interested
in.
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.
const char* QVariant::typeToName ( Type typ ) [static]
Converts the enum representation of the storage type to
its string representation.
SEE ALSO
http://doc.trolltech.com/qvariant.html
COPYRIGHT
Copyright 1992-2001 Trolltech AS,
http://www.trolltech.com. See the license file included
in the distribution for a complete license statement.
AUTHOR
Generated automatically from the source code.
BUGS
If you find a bug in Qt, please report it as described in
http://doc.trolltech.com/bughowto.html. Good bug reports
make our job much simpler. Thank you.
In case of content or formattting problems with this
manual page, please report them to qt-bugs@trolltech.com.
Please include the name of the manual page (qvariant.3qt)
and the Qt version (2.3.1).
Man(1) output converted with
man2html