QVariant Class Reference

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

  1. #include <QVariant>

Inherited by: QDBusVariant.

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:

  1.                 QDataStream out(...);
  2. QVariant v(123);                // The variant now contains an int
  3. int x = v.toInt();              // x = 123
  4. out << v;                       // Writes a type tag and an int to out
  5. v = QVariant("hello");          // The variant now contains a QByteArray
  6. v = QVariant(tr("hello"));      // The variant now contains a QString
  7. int y = v.toInt();              // y = 0 since v cannot be converted to an int
  8. QString s = v.toString();       // s = tr("hello")  (see QObject::tr())
  9. out << v;                       // Writes a type tag and a QString to out
  10. ...
  11. QDataStream in(...);            // (opening the previously written stream)
  12. in >> v;                        // Reads an Int variant
  13. int z = v.toInt();              // z = 123
  14. qDebug("Type is %s",            // prints "Type is int"
  15.         v.typeName());
  16. v = v.toInt() + 100;            // The variant now hold the value 223

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 can have a defined type with no value set. However, note that QVariant types can only be cast when they have had a value set.

  1. QVariant x, y(QString()), z(QString(""));
  2. x.convert(QVariant::Int);
  3. // x.isNull() == true
  4. // y.isNull() == true, z.isNull() == false

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:

  1. QVariant variant;
  2. ...
  3. 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:

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

Using canConvert() and convert() Consecutively

When using canConvert() and convert() consecutively, it is possible for canConvert() to return true, but convert() to return false. This is typically because canConvert() only reports the general ability of QVariant to convert between types given suitable data; it is still possible to supply data which cannot actually be converted.

For example, canConvert() would return true when called on a variant containing a string because, in principle, QVariant is able to convert strings of numbers to integers. However, if the string contains non-numeric characters, it cannot be converted to an integer, and any attempt to convert it will fail. Hence, it is important to have both functions return true for a successful conversion.

See also QMetaType.

Public Types

Toggle detailsenum QVariant::

TypeType { Invalid , Bool , Int , UInt , LongLong , ULongLong , Double , Char , Map , List , String , StringList , ByteArray , BitArray , Date , Time , DateTime , Url , Locale , Rect , RectF , Size , SizeF , Line , LineF , Point , PointF , RegExp , Hash , EasingCurve , LastCoreType , ColorGroup , Font , Pixmap , Brush , Color , Palette , Icon , Image , Polygon , Region , Bitmap , Cursor , SizePolicy , KeySequence , Pen , TextLength , TextFormat , Matrix , Transform , Matrix4x4 , Vector2D , Vector3D , Vector4D , Quaternion , LastGuiType , UserType , IconSet , CString , PointArray , LastType 0xffffffff ...} { Invalid , Bool , Int , UInt , LongLong , ULongLong , Double , Char , Map , List , String , StringList , ByteArray , BitArray , Date , Time , DateTime , Url , Locale , Rect , RectF , Size , SizeF , Line , LineF , Point , PointF , RegExp , Hash , EasingCurve , LastCoreType , ColorGroup , Font , Pixmap , Brush , Color , Palette , Icon , Image , Polygon , Region , Bitmap , Cursor , SizePolicy , KeySequence , Pen , TextLength , TextFormat , Matrix , Transform , Matrix4x4 , Vector2D , Vector3D , Vector4D , Quaternion , LastGuiType , UserType , IconSet , CString , PointArray , LastType 0xffffffff }

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

ConstantValueDescription
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::EasingCurve 29 a QEasingCurve
QVariant::Font 64 a QFont
QVariant::Hash 28 a QVariantHash
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(obsolete)
QVariant::Transform 81 a QTransform
QVariant::Matrix4x4 82 a QMatrix4x4
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::Quaternion 86 a QQuaternion
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::Vector2D 83 a QVector2D
QVariant::Vector3D 84 a QVector3D
QVariant::Vector4D 85 a QVector4D
QVariant::UserType 127 Base value for user-defined types.

Look up this member in the source code.

    Public Functions

    Toggle details QVariant

    QVariantQVariant () ()

    Constructs an invalid variant.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QLocale &l ) ( const QLocale &l )

    Constructs a new variant with a locale value, l.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QRegExp &regExp ) ( const QRegExp &regExp )

    Constructs a new variant with the regexp value regExp.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QEasingCurve &val ) ( const QEasingCurve &val )

    Constructs a new variant with an easing curve value, val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( Qt::GlobalColor color ) ( Qt::GlobalColor color )

    Constructs a new variant of type QVariant::Color and initializes it with color.

    This is a convenience constructor that allows QVariant(Qt::blue); to create a valid QVariant storing a QColor.

    Note: This constructor will assert if the application does not link to the Qt GUI library.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( Type type ) ( Type type )

    Constructs a null variant of type type.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( int typeOrUserType , const void *copy ) ( int typeOrUserType , const void *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.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QVariant &p ) ( const QVariant &p )

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

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( QDataStream &s ) ( QDataStream &s )

    Reads the variant from the data stream, s.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( int val ) ( int val )

    Constructs a new variant with an integer value, val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( uint val ) ( uint val )

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

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( qlonglong val ) ( qlonglong val )

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

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( qulonglong val ) ( qulonglong val )

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

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( bool val ) ( bool val )

    Constructs a new variant with a boolean value, val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( double val ) ( double val )

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

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( floatval ) ( floatval )

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

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const char *val ) ( const char *val )

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

    Note that val is converted to a QString for storing in the variant and QVariant::type() will return QMetaType::QString for the variant.

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

    See also QTextCodec::setCodecForCStrings().

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QByteArray &val ) ( const QByteArray &val )

    Constructs a new variant with a bytearray value, val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QBitArray &val ) ( const QBitArray &val )

    Constructs a new variant with a bitarray value, val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QString &val ) ( const QString &val )

    Constructs a new variant with a string value, val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QLatin1String &val ) ( const QLatin1String &val )

    Constructs a new variant with a string value, val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QStringList &val ) ( const QStringList &val )

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

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QChar &c ) ( const QChar &c )

    Constructs a new variant with a char value, c.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QDate &val ) ( const QDate &val )

    Constructs a new variant with a date value, val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QTime &val ) ( const QTime &val )

    Constructs a new variant with a time value, val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QDateTime &val ) ( const QDateTime &val )

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

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QList <QVariant> &val ) ( const QList <QVariant> &val )

    Constructs a new variant with a list value, val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QMap <QString , QVariant> &val ) ( const QMap <QString , QVariant> &val )

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

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QHash <QString , QVariant> &val ) ( const QHash <QString , QVariant> &val )

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

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QSize &val ) ( const QSize &val )

    Constructs a new variant with a size value of val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QSizeF &val ) ( const QSizeF &val )

    Constructs a new variant with a size value of val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QPoint &val ) ( const QPoint &val )

    Constructs a new variant with a point value of val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QPointF &val ) ( const QPointF &val )

    Constructs a new variant with a point value of val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QLine &val ) ( const QLine &val )

    Constructs a new variant with a line value of val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QLineF &val ) ( const QLineF &val )

    Constructs a new variant with a line value of val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QRect &val ) ( const QRect &val )

    Constructs a new variant with a rect value of val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QRectF &val ) ( const QRectF &val )

    Constructs a new variant with a rect value of val.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( const QUrl &val ) ( const QUrl &val )

    Constructs a new variant with a url value of val.

    Look up this member in the source code.

    Toggle details QVariant

    ~QVariant~QVariant () ()

    Destroys the QVariant and the contained object.

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

    Look up this member in the source code.

    Toggle details bool QVariant

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

    The following casts are done automatically:

    TypeAutomatically 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 String
    UInt Bool, Char, Double, Int, LongLong, String, ULongLong
    ULongLong Bool, Char, Double, Int, LongLong, String, UInt

    See also convert().

    Look up this member in the source code.

    Toggle details bool QVariant

    canConvertcanConvert () ()const

    Returns true if the variant can be converted to the template type T, otherwise false.

    Example:

    1.                     QVariant v = 42;
    2.  
    3. v.canConvert<int>();              // returns true
    4. v.canConvert<QString>();          // returns true
    5.  
    6. MyCustomStruct s;
    7. v.setValue(s);
    8.  
    9. v.canConvert<int>();              // returns false
    10. v.canConvert<MyCustomStruct>();   // returns true

    Warning: This function is not available with MSVC 6. Use qVariantCanConvert() instead if you need to support that version of the compiler.

    See also convert().

    Look up this member in the source code.

    Toggle details void QVariant

    clearclear () ()

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

    Look up this member in the source code.

    Toggle details bool QVariant

    convertconvert ( Type t ) ( Type t )

    Casts the variant to the requested type, t. If the cast cannot be done, the variant is cleared. Returns true if the current type of the variant was successfully cast; otherwise returns false.

    Warning: For historical reasons, converting a null QVariant results in a null value of the desired type (e.g., an empty string for QString) and a result of false.

    See also canConvert() and clear().

    Look up this member in the source code.

    Toggle details QVariant QVariant

    fromValuefromValue ( const T &value ) ( const T &value ) [static]

    Returns a QVariant containing a copy of value. Behaves exactly like setValue() otherwise.

    Example:

    1. MyCustomStruct s;
    2. return QVariant::fromValue(s);

    Note: If you are working with custom types, you should use the Q_DECLARE_METATYPE() macro to register your custom type.

    Warning: This function is not available with MSVC 6. Use qVariantFromValue() instead if you need to support that version of the compiler.

    See also setValue() and value().

    Look up this member in the source code.

    Toggle details bool QVariant

    isNullisNull () ()const

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

    Look up this member in the source code.

    Toggle details bool QVariant

    isValidisValid () ()const

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

    Look up this member in the source code.

    Toggle details Type QVariant

    nameToTypenameToType ( const char *name ) ( const char *name ) [static]

    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.

    Look up this member in the source code.

    Toggle details void QVariant

    setValuesetValue ( const T &value ) ( const T &value )

    Stores a copy of value. If T is a type that QVariant doesn't support, QMetaType is used to store the value. A compile error will occur if QMetaType doesn't handle the type.

    Example:

    1.                     QVariant v;
    2.  
    3. v.setValue(5);
    4. int i = v.toInt();         // i is now 5
    5. QString s = v.toString()   // s is now "5"
    6.  
    7. MyCustomStruct c;
    8. v.setValue(c);
    9.  
    10. ...
    11.  
    12. MyCustomStruct c2 = v.value<MyCustomStruct>();

    Warning: This function is not available with MSVC 6. Use qVariantSetValue() instead if you need to support that version of the compiler.

    See also value(), fromValue(), and canConvert().

    Look up this member in the source code.

    Toggle details QBitArray QVariant

    toBitArraytoBitArray () ()const

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

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details bool QVariant

    toBooltoBool () ()const

    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 or ByteArray and its lower-case content is not empty, "0" or "false"; otherwise returns false.

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details QByteArray QVariant

    toByteArraytoByteArray () ()const

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

    Look up this member in the source code.

    Toggle details QChar QVariant

    toChartoChar () ()const

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

    Look up this member in the source code.

    Toggle details QDate QVariant

    toDatetoDate () ()const

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

    Look up this member in the source code.

    Toggle details QDateTime QVariant

    toDateTimetoDateTime () ()const

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

    Look up this member in the source code.

    Toggle details double QVariant

    toDoubletoDouble ( bool *ok=0 ) ( bool *ok=0 )const

    Returns the variant as a double if the variant has type() Double, QMetaType::Float, 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().

    Look up this member in the source code.

    Toggle details QEasingCurve QVariant

    toEasingCurvetoEasingCurve () ()const

    Returns the variant as a QEasingCurve if the variant has type() EasingCurve; otherwise returns a default easing curve.

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details float QVariant

    toFloattoFloat ( bool *ok=0 ) ( bool *ok=0 )const

    Returns the variant as a float if the variant has type() Double, QMetaType::Float, 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().

    Look up this member in the source code.

    Toggle details QHash <QString , QVariant> QVariant

    toHashtoHash () ()const

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

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details int QVariant

    toInttoInt ( bool *ok=0 ) ( bool *ok=0 )const

    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.

    Warning: If the value is convertible to a LongLong but is too large to be represented in an int, the resulting arithmetic overflow will not be reflected in ok. A simple workaround is to use QString::toInt(). Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details QLine QVariant

    toLinetoLine () ()const

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

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details QLineF QVariant

    toLineFtoLineF () ()const

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

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details QList <QVariant> QVariant

    toListtoList () ()const

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

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details QLocale QVariant

    toLocaletoLocale () ()const

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

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details qlonglong QVariant

    toLongLongtoLongLong ( bool *ok=0 ) ( bool *ok=0 )const

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

    Look up this member in the source code.

    Toggle details QMap <QString , QVariant> QVariant

    toMaptoMap () ()const

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

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details QPoint QVariant

    toPointtoPoint () ()const

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

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details QPointF QVariant

    toPointFtoPointF () ()const

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

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details qreal QVariant

    toRealtoReal ( bool *ok=0 ) ( bool *ok=0 )const

    Returns the variant as a qreal if the variant has type() Double, QMetaType::Float, 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().

    Look up this member in the source code.

    Toggle details QRect QVariant

    toRecttoRect () ()const

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

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details QRectF QVariant

    toRectFtoRectF () ()const

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

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details QRegExp QVariant

    toRegExptoRegExp () ()const

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

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details QSize QVariant

    toSizetoSize () ()const

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

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details QSizeF QVariant

    toSizeFtoSizeF () ()const

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

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details QString QVariant

    toStringtoString () ()const

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

    Look up this member in the source code.

    Toggle details QStringList QVariant

    toStringListtoStringList () ()const

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

    Look up this member in the source code.

    Toggle details QTime QVariant

    toTimetoTime () ()const

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

    Look up this member in the source code.

    Toggle details uint QVariant

    toUInttoUInt ( bool *ok=0 ) ( bool *ok=0 )const

    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.

    Warning: If the value is convertible to a ULongLong but is too large to be represented in an unsigned int, the resulting arithmetic overflow will not be reflected in ok. A simple workaround is to use QString::toUInt(). Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details qulonglong QVariant

    toULongLongtoULongLong ( bool *ok=0 ) ( bool *ok=0 )const

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

    Look up this member in the source code.

    Toggle details QUrl QVariant

    toUrltoUrl () ()const

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

    See also canConvert() and convert().

    Look up this member in the source code.

    Toggle details Type QVariant

    typetype () ()const

    Returns the storage type of the value stored in the variant. Although this function is declared as returning QVariant::Type, the return value should be interpreted as QMetaType::Type. In particular, QVariant::UserType is returned here only if the value is equal or greater than QMetaType::User.

    Note that return values in the ranges QVariant::Char through QVariant::RegExp and QVariant::Font through QVariant::Transform correspond to the values in the ranges QMetaType::QChar through QMetaType::QRegExp and QMetaType::QFont through QMetaType::QQuaternion.

    Pay particular attention when working with char and QChar variants. Note that there is no QVariant constructor specifically for type char, but there is one for QChar. For a variant of type QChar, this function returns QVariant::Char, which is the same as QMetaType::QChar, but for a variant of type char, this function returns QMetaType::Char, which is not the same as QVariant::Char.

    Also note that the types void*, long, short, unsigned long, unsigned short, unsigned char, float, QObject*, and QWidget* are represented in QMetaType::Type but not in QVariant::Type, and they can be returned by this function. However, they are considered to be user defined types when tested against QVariant::Type.

    To test whether an instance of QVariant contains a data type that is compatible with the data type you are interested in, use canConvert().

    Look up this member in the source code.

    Toggle details const char * QVariant

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

    Look up this member in the source code.

    Toggle details const char * QVariant

    typeToNametypeToName ( Type typ ) ( Type typ ) [static]

    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.

    Look up this member in the source code.

    Toggle details int QVariant

    userTypeuserType () ()const

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

    See also type().

    Look up this member in the source code.

    Toggle details T QVariant

    valuevalue () ()const

    Returns the stored value converted to the template type T. Call canConvert() to find out whether a type can be converted. If the value cannot be converted, default-constructed value will be returned.

    If the type T is supported by QVariant, this function behaves exactly as toString(), toInt() etc.

    Example:

    1.                     QVariant v;
    2.  
    3. MyCustomStruct c;
    4. if (v.canConvert<MyCustomStruct>())
    5.     c = v.value<MyCustomStruct>();
    6.  
    7. v = 7;
    8. int i = v.value<int>();                        // same as v.toInt()
    9. QString s = v.value<QString>();                // same as v.toString(), s is now "7"
    10. MyCustomStruct c2 = v.value<MyCustomStruct>(); // conversion failed, c2 is empty

    Warning: This function is not available with MSVC 6. Use qVariantValue() or qvariant_cast() instead if you need to support that version of the compiler.

    See also setValue(), fromValue(), and canConvert().

    Look up this member in the source code.

    Toggle details bool QVariant

    operator!=operator!= ( const QVariant &v ) ( const QVariant &v )const

    Compares this QVariant with v and returns true if they are not equal; otherwise returns false.

    Warning: This function doesn't support custom types registered with qRegisterMetaType().

    Look up this member in the source code.

    Toggle details QVariant & QVariant

    operator=operator= ( const QVariant &variant ) ( const QVariant &variant )

    Assigns the value of the variant variant to this variant.

    Look up this member in the source code.

    Toggle details bool QVariant

    operator==operator== ( const QVariant &v ) ( const QVariant &v )const

    Compares this QVariant with v and returns true if they are equal; otherwise returns false.

    In the case of custom types, their equalness operators are not called. Instead the values' addresses are compared.

    Look up this member in the source code.

    Toggle details QVariant

    QVariantQVariant ( bool b , int dummy ) ( bool b , int dummy ) Qt3-support

    Use the QVariant(bool) constructor instead.

    Look up this member in the source code.

    Toggle details QBitArray & QVariant

    asBitArrayasBitArray () () Qt3-support

    Use toBitArray() instead.

    Look up this member in the source code.

    Toggle details bool & QVariant

    asBoolasBool () () Qt3-support

    Use toBool() instead.

    Look up this member in the source code.

    Toggle details QByteArray & QVariant

    asByteArrayasByteArray () () Qt3-support

    Use toByteArray() instead.

    Look up this member in the source code.

    Toggle details QByteArray & QVariant

    asCStringasCString () () Qt3-support

    Use toByteArray() instead.

    Look up this member in the source code.

    Toggle details QDate & QVariant

    asDateasDate () () Qt3-support

    Use toDate() instead.

    Look up this member in the source code.

    Toggle details QDateTime & QVariant

    asDateTimeasDateTime () () Qt3-support

    Use toDateTime() instead.

    Look up this member in the source code.

    Toggle details double & QVariant

    asDoubleasDouble () () Qt3-support

    Use toDouble() instead.

    Look up this member in the source code.

    Toggle details int & QVariant

    asIntasInt () () Qt3-support

    Use toInt() instead.

    Look up this member in the source code.

    Toggle details QList <QVariant> & QVariant

    asListasList () () Qt3-support

    Use toList() instead.

    Look up this member in the source code.

    Toggle details qlonglong & QVariant

    asLongLongasLongLong () () Qt3-support

    Use toLongLong() instead.

    Look up this member in the source code.

    Toggle details QMap <QString , QVariant> & QVariant

    asMapasMap () () Qt3-support

    Use toMap() instead.

    Look up this member in the source code.

    Toggle details QPoint & QVariant

    asPointasPoint () () Qt3-support

    Use toPoint() instead.

    Look up this member in the source code.

    Toggle details QRect & QVariant

    asRectasRect () () Qt3-support

    Use toRect() instead.

    Look up this member in the source code.

    Toggle details QSize & QVariant

    asSizeasSize () () Qt3-support

    Use toSize() instead.

    Look up this member in the source code.

    Toggle details QString & QVariant

    asStringasString () () Qt3-support

    Use toString() instead.

    Look up this member in the source code.

    Toggle details QStringList & QVariant

    asStringListasStringList () () Qt3-support

    Use toStringList() instead.

    Look up this member in the source code.

    Toggle details QTime & QVariant

    asTimeasTime () () Qt3-support

    Use toTime() instead.

    Look up this member in the source code.

    Toggle details uint & QVariant

    asUIntasUInt () () Qt3-support

    Use toUInt() instead.

    Look up this member in the source code.

    Toggle details qulonglong & QVariant

    asULongLongasULongLong () () Qt3-support

    Use toULongLong() instead.

    Look up this member in the source code.

    Toggle details bool QVariant

    canCastcanCast ( Type t ) ( Type t )const Qt3-support

    Use canConvert() instead.

    Look up this member in the source code.

    Toggle details bool QVariant

    castcast ( Type t ) ( Type t ) Qt3-support

    Use convert() instead.

    Look up this member in the source code.

    Toggle details const QByteArray QVariant

    toCStringtoCString () ()const Qt3-support

    Use toByteArray() instead.

    Look up this member in the source code.

    Notes provided by the Qt Community
    Informative
    • 5

    Votes: 4

    Coverage: Qt library 4.7, 4.8, 5.0

    Picture of Andre Andre

    Robot Herder
    30 notes

    Nokia Certified Qt Developer

    Implicit sharing

    Note that QVariant is an implicitly shared [developer.qt.nokia.com] class. That means that copying QVariants around is quite cheap, as long as you don’t modify them.

    [Revisions]

    Informative
    • 5

    Votes: 2

    Coverage: Qt library 4.7, 4.8, 5.0

    Picture of tucnak tucnak

    Ant Farmer
    9 notes

    QVariant with your custom classes

    If you want to give QVariant opportunity to use your class object, just use Q_DECLARE_METATYPE after your class defenition:

    1. class CustomClass {
    2.    // class members
    3. };
    4. Q_DECLARE_METATYPE(CustomClass);

    [Revisions]