Acts like a union for the most common Qt data types. More...
#include <qvariant.h>
Inherits QShared.
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 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.
Constructs an invalid variant.
Reads the variant from the data stream.
Constructs a new variant with a boolean value.
Constructs a new variant with a brush value.
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.
Constructs a new variant with a font value.
Constructs a new variant with an empty iconset.
Constructs a new variant with an image value.
Constructs a new variant with a map of QVariants.
Constructs a new variant with a color palette value.
Constructs a new variant with a pixmap value.
Constructs a new variant with a point value.
Constructs a new variant with a rect value.
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 deep copy of the variant passed as argument to this
constructor.
Constructs a new variant with a c-string value.
Constructs a new variant with a floating point value.
Constructs a new variant with an 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.
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:
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.
Internal function for loading a variant. Use the stream operators
instead.
Converts the string representation of the storage type to
its enum representation.
Assigns the value of some other variant to this variant.
This is a deep copy.
Internal function for saving a variant. Use the stream operators
instead.
Changes the value of this variant to val.
This is not called setValue(bool), since some compilers
cannot distinguish between bool and int.
Changes the value of this variant to val.
Changes the value of this variant to val.
Changes the value of this variant to val.
Changes the value of this variant to val.
Changes the value of this variant to val.
Changes the value of this variant to val.
Changes the value of this variant to 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.
Changes the value of this variant to val.
Changes the value of this variant to val.
Changes the value of this variant to val.
Changes the value of this variant to val.
Changes the value of this variant to val.
Changes the value of this variant to 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.
Changes the value of this variant to val.
This function creates a copy of the list.
Changes the value of this variant to val.
The Variant creates a copy of the passed string.
Changes the value of this variant to val.
Changes the value of this variant to val.
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.
Returns the variant as a QBrush if the variant has type()
Brush, or a default brush otherwise.
Returns the variant as a QCString if the variant has type()
CString, or a null QCString otherwise.
Returns the variant as a QColor if the variant has type()
Color, or an invalid color otherwise.
Returns the variant as a QColorGroup if the variant has type()
ColorGroup, or a completely black color group otherwise.
Returns the variant as a double if the variant has type()
Double, Int or Bool, or 0.0 otherwise.
Returns the variant as a QFont if the variant has type()
Font, or the default font otherwise.
Returns the variant as a QIconSet if the variant has type()
IconSet, or an icon set of null pixmaps otherwise.
Returns the variant as a QImage if the variant has type()
Image, or a null image otherwise.
Returns the variant as an int if the variant has type()
Int, Double or Bool, or 0 otherwise.
Returns the variant as a QValueList Returns the variant as a QPalette if the variant has type()
Palette, or a completely black palette otherwise.
Returns the variant as a QPixmap if the variant has type()
Pixmap, or a null pixmap otherwise.
Returns the variant as a QPoint if the variant has type()
Point, or a the point (0,0) otherwise.
Returns the variant as a QRect if the variant has type()
Rect, or an empty rectangle otherwise.
Returns the variant as a QSize if the variant has type()
Size, or an invalid size otherwise.
Returns the variant as a QString if the variant has type()
String or CString, or QString::null otherwise.
Returns the variant as a QStringList if the variant has type()
StringList or List, or an empty list otherwise.
Returns the stoarge type of the value stored in the
variant currently.
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.troll.no):
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
Bool
- a bool
Double
- a doublea
CString
- a QCString
Member Function Documentation
QVariant::QVariant ()
QVariant::QVariant ( QDataStream & s )
QVariant::QVariant ( bool val )
QVariant::QVariant ( const QBrush & val )
QVariant::QVariant ( const QColor & val )
QVariant::QVariant ( const QColorGroup & val )
QVariant::QVariant ( const QCString & 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 QRect & 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 ()
[virtual]
bool QVariant::canCast ( Type t ) const
void QVariant::clear ()
[virtual protected]
bool QVariant::isValid () const
void QVariant::load ( QDataStream & s )
[virtual]
QVariant::Type QVariant::nameToType( const char * name )
[static]
QVariant& QVariant::operator= ( const QVariant & other )
void QVariant::save ( QDataStream & s ) const
[virtual]
void QVariant::setBoolValue ( bool val )
void QVariant::setValue ( const QBrush & val )
void QVariant::setValue ( const QColor & val )
void QVariant::setValue ( const QColorGroup & val )
void QVariant::setValue ( const QCString & val )
void QVariant::setValue ( const QFont & val )
void QVariant::setValue ( const QIconSet & val )
void QVariant::setValue ( const QImage & val )
void QVariant::setValue ( const QMap<QString,QVariant> & val )
void QVariant::setValue ( const QPalette & val )
void QVariant::setValue ( const QPixmap & val )
void QVariant::setValue ( const QPoint & val )
void QVariant::setValue ( const QRect & val )
void QVariant::setValue ( const QSize & val )
void QVariant::setValue ( const QString & val )
void QVariant::setValue ( const QStringList & val )
void QVariant::setValue ( const QValueList<QVariant> & val )
void QVariant::setValue ( const char * val )
void QVariant::setValue ( double val )
void QVariant::setValue ( int val )
bool QVariant::toBool () const
QBrush QVariant::toBrush () const
QCString QVariant::toCString () const
QColor QVariant::toColor () const
QColorGroup QVariant::toColorGroup () const
double QVariant::toDouble () const
QFont QVariant::toFont () const
QIconSet QVariant::toIconSet () const
QImage QVariant::toImage () const
int QVariant::toInt () const
QValueList<QVariant> QVariant::toList () const
QPalette QVariant::toPalette () const
QPixmap QVariant::toPixmap () const
QPoint QVariant::toPoint () const
QRect QVariant::toRect () const
QSize QVariant::toSize () const
QString QVariant::toString () const
QStringList QVariant::toStringList () const
Type QVariant::type () const
const char* QVariant::typeName () const
[virtual]
const char* QVariant::typeToName ( Type typ )
[static]
Copyright İ 2000 Troll Tech Trademarks