Home · All Classes · Modules

QVariant Class Reference
[QtCore module]

The QVariant class acts like a union for the most common Qt data types. More...

Types

Methods

Static Methods

Special Methods


Detailed Description

The QVariant class acts like a union for the most common Qt data types.

Because C++ forbids unions from including types that have non-default constructors or destructors, most interesting Qt classes cannot be used in unions. Without QVariant, this would be a problem for QObject.property() and for database work, etc.

A QVariant object holds a single value of a single type() at a time. (Some type()s are multi-valued, for example a string list.) You can find out what type, T, the variant holds, convert it to a different type using convert(), 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 canConvert().

The methods named toT() (e.g., toInt(), toString()) 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.

Here is some 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 QByteArray
 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 = v.toInt() + 100;            // The variant now hold the value 223
 v = QVariant(QStringList());

You can even store QList<QVariant> and QMap<QString, QVariant> values in a variant, so you can easily construct arbitrarily complex data structures of arbitrary types. This is very powerful and versatile, but may prove less memory and speed efficient than storing specific types in standard data structures.

QVariant also supports the notion of null values, where you have a defined type with no value set.

 QVariant x, y(QString()), z(QString(""));
 x.convert(QVariant.Int);
 // x.isNull() == true
 // y.isNull() == true, z.isNull() == false
 // y.isEmpty() == true, z.isEmpty() == true

QVariant can be extended to support other types than those mentioned in the Type enum. See the QMetaType documentation for details.

A Note on GUI Types

Because QVariant is part of the QtCore library, it cannot provide conversion functions to data types defined in QtGui, such as QColor, QImage, and QPixmap. In other words, there is no toColor() function. Instead, you can use the QVariant.value() or the qVariantValue() template function. For example:

 QVariant variant;
 ...
 QColor color = variant.value<QColor>();

The inverse conversion (e.g., from QColor to QVariant) is automatic for all data types supported by QVariant, including GUI-related types:

 QColor color = palette().background().color();
 QVariant variant = color;

See also QMetaType.


Type Documentation

QVariant.Type

This enum type defines the types of variable that a QVariant can contain.

Constant Value Description
QVariant.Invalid 0 no type
QVariant.BitArray 13 a QBitArray
QVariant.Bitmap 73 a QBitmap
QVariant.Bool 1 a bool
QVariant.Brush 66 a QBrush
QVariant.ByteArray 12 a QByteArray
QVariant.Char 7 a QChar
QVariant.Color 67 a QColor
QVariant.Cursor 74 a QCursor
QVariant.Date 14 a QDate
QVariant.DateTime 16 a QDateTime
QVariant.Double 6 a double
QVariant.Font 64 a QFont
QVariant.Icon 69 a QIcon
QVariant.Image 70 a QImage
QVariant.Int 2 an int
QVariant.KeySequence 76 a QKeySequence
QVariant.Line 23 a QLine
QVariant.LineF 24 a QLineF
QVariant.List 9 a QVariantList
QVariant.Locale 18 a QLocale
QVariant.LongLong 4 a qlonglong
QVariant.Map 8 a QVariantMap
QVariant.Matrix 80 a QMatrix
QVariant.Transform 81 a QTransform
QVariant.Palette 68 a QPalette
QVariant.Pen 77 a QPen
QVariant.Pixmap 65 a QPixmap
QVariant.Point 25 a QPoint
QVariant.PointArray Polygon a QPointArray
QVariant.PointF 26 a QPointF
QVariant.Polygon 71 a QPolygon
QVariant.Rect 19 a QRect
QVariant.RectF 20 a QRectF
QVariant.RegExp 27 a QRegExp
QVariant.Region 72 a QRegion
QVariant.Size 21 a QSize
QVariant.SizeF 22 a QSizeF
QVariant.SizePolicy 75 a QSizePolicy
QVariant.String 10 a QString
QVariant.StringList 11 a QStringList
QVariant.TextFormat 79 a QTextFormat
QVariant.TextLength 78 a QTextLength
QVariant.Time 15 a QTime
QVariant.UInt 3 a uint
QVariant.ULongLong 5 a qulonglong
QVariant.Url 17 a QUrl
QVariant.UserType 127 Base value for user-defined types.



Method Documentation

QVariant.__init__ (self)

Constructs an invalid variant.

QVariant.__init__ (self, QPixmap)

This method is only available if the QtGui module is imported.

Constructs an invalid variant.

QVariant.__init__ (self, Type type)

Constructs a null variant of type type.

QVariant.__init__ (self, QBitmap)

This method is only available if the QtGui module is imported.

Constructs a null variant of type type.

QVariant.__init__ (self, int typeOrUserType, sip.voidptr copy)

Constructs variant of type typeOrUserType, and initializes with copy if copy is not 0.

Note that you have to pass the address of the variable you want stored.

Usually, you never have to use this constructor, use qVariantFromValue() instead to construct variants from the pointer types represented by QMetaType.VoidStar, QMetaType.QObjectStar and QMetaType.QWidgetStar.

See also qVariantFromValue() and Type.

QVariant.__init__ (self, QColor)

This method is only available if the QtGui module is imported.

Constructs variant of type typeOrUserType, and initializes with copy if copy is not 0.

Note that you have to pass the address of the variable you want stored.

Usually, you never have to use this constructor, use qVariantFromValue() instead to construct variants from the pointer types represented by QMetaType.VoidStar, QMetaType.QObjectStar and QMetaType.QWidgetStar.

See also qVariantFromValue() and Type.

QVariant.__init__ (self, QVariant other)

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

QVariant.__init__ (self, QBrush)

This method is only available if the QtGui module is imported.

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

QVariant.__init__ (self, QDataStream s)

Reads the variant from the data stream, s.

QVariant.__init__ (self, QCursor)

This method is only available if the QtGui module is imported.

Reads the variant from the data stream, s.

QVariant.__init__ (self, bool b)

Constructs a new variant with an integer value, val.

QVariant.__init__ (self, QFont)

This method is only available if the QtGui module is imported.

Constructs a new variant with an integer value, val.

QVariant.__init__ (self, int i)

Constructs a new variant with an unsigned integer value, val.

QVariant.__init__ (self, QIcon)

This method is only available if the QtGui module is imported.

Constructs a new variant with an unsigned integer value, val.

QVariant.__init__ (self, float d)

Constructs a new variant with a long long integer value, val.

QVariant.__init__ (self, QImage)

This method is only available if the QtGui module is imported.

Constructs a new variant with a long long integer value, val.

QVariant.__init__ (self, long ll)

Constructs a new variant with an unsigned long long integer value, val.

QVariant.__init__ (self, QKeySequence)

This method is only available if the QtGui module is imported.

Constructs a new variant with an unsigned long long integer value, val.

QVariant.__init__ (self, unsigned long ull)

Constructs a new variant with a boolean value, val. The integer argument is a dummy, necessary for compatibility with some compilers.

QVariant.__init__ (self, QMatrix)

This method is only available if the QtGui module is imported.

Constructs a new variant with a boolean value, val. The integer argument is a dummy, necessary for compatibility with some compilers.

QVariant.__init__ (self, QString string)

Constructs a new variant with a floating point value, val.

QVariant.__init__ (self, QPalette)

This method is only available if the QtGui module is imported.

Constructs a new variant with a floating point value, val.

QVariant.__init__ (self, QByteArray bytearray)

Constructs a new variant with a string value of val. The variant creates a deep copy of val, using the encoding set by QTextCodec.setCodecForCStrings().

You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications.

See also QTextCodec.setCodecForCStrings().

QVariant.__init__ (self, QPen)

This method is only available if the QtGui module is imported.

Constructs a new variant with a string value of val. The variant creates a deep copy of val, using the encoding set by QTextCodec.setCodecForCStrings().

You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications.

See also QTextCodec.setCodecForCStrings().

QVariant.__init__ (self, QBitArray bitarray)

Constructs a new variant with a bytearray value, val.

QVariant.__init__ (self, QPolygon)

This method is only available if the QtGui module is imported.

Constructs a new variant with a bytearray value, val.

QVariant.__init__ (self, QLatin1String string)

Constructs a new variant with a bitarray value, val.

QVariant.__init__ (self, QRegion)

This method is only available if the QtGui module is imported.

Constructs a new variant with a bitarray value, val.

QVariant.__init__ (self, QStringList stringlist)

Constructs a new variant with a string value, val.

QVariant.__init__ (self, QSizePolicy)

This method is only available if the QtGui module is imported.

Constructs a new variant with a string value, val.

QVariant.__init__ (self, QChar qchar)

Constructs a new variant with a string value, val.

QVariant.__init__ (self, QTextLength)

This method is only available if the QtGui module is imported.

Constructs a new variant with a string value, val.

QVariant.__init__ (self, QDate date)

Constructs a new variant with a string list value, val.

QVariant.__init__ (self, QTextFormat)

This method is only available if the QtGui module is imported.

Constructs a new variant with a string list value, val.

QVariant.__init__ (self, QTime time)

Constructs a new variant with a char value, c.

QVariant.__init__ (self, QTransform)

This method is only available if the QtGui module is imported.

Constructs a new variant with a char value, c.

QVariant.__init__ (self, QDateTime datetime)

Constructs a new variant with a date value, val.

QVariant.__init__ (self, QVariant-list list)

Constructs a new variant with a time value, val.

QVariant.__init__ (self, QString-QVariant-dict map)

Constructs a new variant with a date/time value, val.

QVariant.__init__ (self, QSize size)

Constructs a new variant with a list value, val.

QVariant.__init__ (self, QSizeF size)

Constructs a new variant with a map of QVariants, val.

QVariant.__init__ (self, QPoint pt)

Constructs a new variant with a size value of val.

QVariant.__init__ (self, QPointF pt)

Constructs a new variant with a size value of val.

QVariant.__init__ (self, QLine line)

Constructs a new variant with a point value of val.

QVariant.__init__ (self, QLineF line)

Constructs a new variant with a point value of val.

QVariant.__init__ (self, QRect rect)

Constructs a new variant with a line value of val.

QVariant.__init__ (self, QRectF rect)

Constructs a new variant with a line value of val.

QVariant.__init__ (self, QUrl url)

Constructs a new variant with a rect value of val.

QVariant.__init__ (self, QLocale locale)

Constructs a new variant with a rect value of val.

QVariant.__init__ (self, QRegExp regExp)

Constructs a new variant with a url value of val.

QVariant.__init__ (self, Qt.GlobalColor color)

Constructs a new variant with a locale value, l.

bool QVariant.canConvert (self, Type t)

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(), ... methods.

The following casts are done automatically:

Type Automatically Cast To
Bool Char, Double, Int, LongLong, String, UInt, ULongLong
ByteArray Double, Int, LongLong, String, UInt, ULongLong
Char Bool, Int, UInt, LongLong, ULongLong
Color String
Date DateTime, String
DateTime Date, String, Time
Double Bool, Int, LongLong, String, UInt, ULongLong
Font String
Int Bool, Char, Double, LongLong, String, UInt, ULongLong
KeySequence Int, String
List StringList (if the list's items can be converted to strings)
LongLong Bool, ByteArray, Char, Double, Int, String, UInt, ULongLong
Point PointF
Rect RectF
String Bool, ByteArray, Char, Color, Date, DateTime, Double, Font, Int, KeySequence, LongLong, StringList, Time, UInt, ULongLong
StringList List, String (if the list contains exactly one item)
Time DateTime, String
UInt Bool, Char, Double, Int, LongLong, String, ULongLong
ULongLong Bool, Char, Double, Int, LongLong, String, UInt


See also convert().

QVariant.clear (self)

Convert this variant to type Invalid and free up any resources used.

bool QVariant.convert (self, 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.Polygon, etc). Returns true if the current type of the variant was successfully cast; otherwise returns false.

See also canConvert().

sip.voidptr QVariant.data (self)

QVariant.detach (self)

bool QVariant.isDetached (self)

bool QVariant.isNull (self)

Returns true if this is a NULL variant, false otherwise.

bool QVariant.isValid (self)

Returns true if the storage type of this variant is not QVariant.Invalid; otherwise returns false.

QVariant.load (self, QDataStream ds)

Type QVariant.nameToType (str name)

Converts the string representation of the storage type given in name, to its enum representation.

If the string representation cannot be converted to any enum representation, the variant is set to Invalid.

QVariant.save (self, QDataStream ds)

QBitArray QVariant.toBitArray (self)

Returns the variant as a QBitArray if the variant has type() BitArray; otherwise returns an empty bit array.

See also canConvert() and convert().

bool QVariant.toBool (self)

Returns the variant as a bool if the variant has type() Bool.

Returns true if the variant has type() Bool, Char, Double, Int, LongLong, UInt, or ULongLong and the value is non-zero, or if the variant has type String and its lower-case content is not empty, "0" or "false"; otherwise returns false.

See also canConvert() and convert().

QByteArray QVariant.toByteArray (self)

Returns the variant as a QByteArray if the variant has type() ByteArray or String (converted using QString.fromAscii()); otherwise returns an empty byte array.

See also canConvert() and convert().

QChar QVariant.toChar (self)

Returns the variant as a QChar if the variant has type() Char, Int, or UInt; otherwise returns an invalid QChar.

See also canConvert() and convert().

QDate QVariant.toDate (self)

Returns the variant as a QDate if the variant has type() Date, DateTime, or String; otherwise returns an invalid date.

If the type() is String, an invalid date will be returned if the string cannot be parsed as a Qt.ISODate format date.

See also canConvert() and convert().

QDateTime QVariant.toDateTime (self)

Returns the variant as a QDateTime if the variant has type() DateTime, Date, or String; otherwise returns an invalid date/time.

If the type() is String, an invalid date/time will be returned if the string cannot be parsed as a Qt.ISODate format date/time.

See also canConvert() and convert().

(float, bool ok) QVariant.toDouble (self)

Returns the variant as a double if the variant has type() Double, Bool, ByteArray, Int, LongLong, String, UInt, or ULongLong; otherwise returns 0.0.

If ok is non-null: *ok is set to true if the value could be converted to a double; otherwise *ok is set to false.

See also canConvert() and convert().

(int, bool ok) QVariant.toInt (self)

Returns the variant as an int if the variant has type() Int, Bool, ByteArray, Char, Double, LongLong, String, UInt, or ULongLong; otherwise returns 0.

If ok is non-null: *ok is set to true if the value could be converted to an int; otherwise *ok is set to false.

See also canConvert() and convert().

QLine QVariant.toLine (self)

Returns the variant as a QLine if the variant has type() Line; otherwise returns an invalid QLine.

See also canConvert() and convert().

QLineF QVariant.toLineF (self)

Returns the variant as a QLineF if the variant has type() LineF; otherwise returns an invalid QLineF.

See also canConvert() and convert().

QVariant-list QVariant.toList (self)

Returns the variant as a QVariantList if the variant has type() List or StringList; otherwise returns an empty list.

See also canConvert() and convert().

QLocale QVariant.toLocale (self)

Returns the variant as a QLocale if the variant has type() Locale; otherwise returns an invalid QLocale.

See also canConvert() and convert().

(long, bool ok) QVariant.toLongLong (self)

Returns the variant as a long long int if the variant has type() LongLong, Bool, ByteArray, Char, Double, Int, String, UInt, or ULongLong; otherwise returns 0.

If ok is non-null: *ok is set to true if the value could be converted to an int; otherwise *ok is set to false.

See also canConvert() and convert().

QString-QVariant-dict QVariant.toMap (self)

Returns the variant as a QMap<QString, QVariant> if the variant has type() Map; otherwise returns an empty map.

See also canConvert() and convert().

QPoint QVariant.toPoint (self)

Returns the variant as a QPoint if the variant has type() Point or PointF; otherwise returns a null QPoint.

See also canConvert() and convert().

QPointF QVariant.toPointF (self)

Returns the variant as a QPointF if the variant has type() Point or PointF; otherwise returns a null QPointF.

See also canConvert() and convert().

QRect QVariant.toRect (self)

Returns the variant as a QRect if the variant has type() Rect; otherwise returns an invalid QRect.

See also canConvert() and convert().

QRectF QVariant.toRectF (self)

Returns the variant as a QRectF if the variant has type() Rect or RectF; otherwise returns an invalid QRectF.

See also canConvert() and convert().

QRegExp QVariant.toRegExp (self)

Returns the variant as a QRegExp if the variant has type() RegExp; otherwise returns an empty QRegExp.

This function was introduced in Qt 4.1.

See also canConvert() and convert().

QSize QVariant.toSize (self)

Returns the variant as a QSize if the variant has type() Size; otherwise returns an invalid QSize.

See also canConvert() and convert().

QSizeF QVariant.toSizeF (self)

Returns the variant as a QSizeF if the variant has type() SizeF; otherwise returns an invalid QSizeF.

See also canConvert() and convert().

QString QVariant.toString (self)

Returns the variant as a QString if the variant has type() String, Bool, ByteArray, Char, Date, DateTime, Double, Int, LongLong, StringList, Time, UInt, or ULongLong; otherwise returns an empty string.

See also canConvert() and convert().

QStringList QVariant.toStringList (self)

Returns the variant as a QStringList if the variant has type() StringList, String, or List of a type that can be converted to QString; otherwise returns an empty list.

See also canConvert() and convert().

QTime QVariant.toTime (self)

Returns the variant as a QTime if the variant has type() Time, DateTime, or String; otherwise returns an invalid time.

If the type() is String, an invalid time will be returned if the string cannot be parsed as a Qt.ISODate format time.

See also canConvert() and convert().

(unsigned long, bool ok) QVariant.toUInt (self)

Returns the variant as an unsigned int if the variant has type() UInt, Bool, ByteArray, Char, Double, Int, LongLong, String, or ULongLong; otherwise returns 0.

If ok is non-null: *ok is set to true if the value could be converted to an unsigned int; otherwise *ok is set to false.

See also canConvert() and convert().

(unsigned long, bool ok) QVariant.toULongLong (self)

Returns the variant as as an unsigned long long int if the variant has type() ULongLong, Bool, ByteArray, Char, Double, Int, LongLong, String, or UInt; otherwise returns 0.

If ok is non-null: *ok is set to true if the value could be converted to an int; otherwise *ok is set to false.

See also canConvert() and convert().

QUrl QVariant.toUrl (self)

Returns the variant as a QUrl if the variant has type() Url; otherwise returns an invalid QUrl.

See also canConvert() and convert().

Type QVariant.type (self)

Returns the storage type of the value stored in the variant. Usually it's best to test with canConvert() whether the variant can deliver the data type you are interested in.

str QVariant.typeName (self)

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

str QVariant.typeToName (Type type)

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

Returns a null pointer if the type is QVariant.Invalid or doesn't exist.

int QVariant.userType (self)

Returns the storage type of the value stored in the variant. For non-user types, this is the same as type().

See also type().

bool QVariant.__eq__ (self, QVariant v)

bool QVariant.__ne__ (self, QVariant v)


PyQt 4.3.1 for X11Copyright © Riverbank Computing Ltd and Trolltech AS 2007Qt 4.3.0