CopperSpice API  1.9.2
QVariant Class Reference

Stores a single value of almost any data type. More...

Public Types

enum  Type : uint
 

Public Methods

 QVariant ()
 
 QVariant (bool value)
 
 QVariant (const QVariant &other)
 
 QVariant (double value)
 
 QVariant (float value)
 
 QVariant (int value)
 
 QVariant (QBitArray value)
 
 QVariant (QByteArray value)
 
 QVariant (QChar32 value)
 
 QVariant (QDataStream &stream)
 
 QVariant (QDate value)
 
 QVariant (QDateTime value)
 
 QVariant (QEasingCurve value)
 
 QVariant (QHash< QString, QVariant > value)
 
 QVariant (qint64 value)
 
 QVariant (QJsonArray value)
 
 QVariant (QJsonDocument value)
 
 QVariant (QJsonObject value)
 
 QVariant (QJsonValue value)
 
 QVariant (QLine value)
 
 QVariant (QLineF value)
 
 QVariant (QList< QVariant > value)
 
 QVariant (QLocale value)
 
 QVariant (QMap< QString, QVariant > value)
 
 QVariant (QModelIndex value)
 
 QVariant (QMultiHash< QString, QVariant > value)
 
 QVariant (QMultiMap< QString, QVariant > value)
 
 QVariant (QPersistentModelIndex value)
 
 QVariant (QPoint value)
 
 QVariant (QPointF value)
 
 QVariant (QRect value)
 
 QVariant (QRectF value)
 
 QVariant (QRegularExpression8 value)
 
 QVariant (QSize value)
 
 QVariant (QSizeF value)
 
 QVariant (QString value)
 
 QVariant (QString16 value)
 
 QVariant (QStringList value)
 
 QVariant (QTime value)
 
 QVariant (quint64 value)
 
 QVariant (QUrl value)
 
 QVariant (QUuid value)
 
 QVariant (QVariant &&other) = default
 
 QVariant (Type type)
 
 QVariant (uint typeId, const void *copy)
 
 QVariant (uint value)
 
 ~QVariant () = default
 
template<typename T >
bool canConvert () const
 
bool canConvert (uint newType) const
 
void clear ()
 
template<typename T >
bool convert ()
 
bool convert (uint newType)
 
int64_t enumToInt () const
 
uint64_t enumToUInt () const
 
template<typename T >
getData () const
 
template<typename T >
std::optional< T > getDataOr () const
 
bool isEnum () const
 
bool isValid () const
 
std::optional< QVariant > maybeConvert (uint requested_type) const
 
bool operator!= (const QVariant &other) const
 
QVariant & operator= (const QVariant &other)
 
QVariant & operator= (QVariant &&other)
 
bool operator== (const QVariant &other) const
 
void setValue (const QVariant &value)
 
template<typename T >
void setValue (const T &value)
 
void swap (QVariant &other)
 
QBitArray toBitArray () const
 
bool toBool (bool *ok=nullptr) const
 
QByteArray toByteArray () const
 
QChar32 toChar () const
 
QDate toDate () const
 
QDateTime toDateTime () const
 
double toDouble (bool *ok=nullptr) const
 
QEasingCurve toEasingCurve () const
 
float toFloat (bool *ok=nullptr) const
 
QHash< QString, QVariant > toHash () const
 
int toInt (bool *ok=nullptr) const
 
QJsonArray toJsonArray () const
 
QJsonDocument toJsonDocument () const
 
QJsonObject toJsonObject () const
 
QJsonValue toJsonValue () const
 
QLine toLine () const
 
QLineF toLineF () const
 
QList< QVariant > toList () const
 
QLocale toLocale () const
 
qint64 toLongLong (bool *ok=nullptr) const
 
QMap< QString, QVariant > toMap () const
 
QModelIndex toModelIndex () const
 
QMultiHash< QString, QVariant > toMultiHash () const
 
QMultiMap< QString, QVariant > toMultiMap () const
 
QPersistentModelIndex toPersistentModelIndex () const
 
QPoint toPoint () const
 
QPointF toPointF () const
 
qreal toReal (bool *ok=nullptr) const
 
QRect toRect () const
 
QRectF toRectF () const
 
QRegularExpression8 toRegularExpression () const
 
QSize toSize () const
 
QSizeF toSizeF () const
 
QString8 toString () const
 
QString16 toString16 () const
 
QStringList toStringList () const
 
QTime toTime () const
 
uint toUInt (bool *ok=nullptr) const
 
quint64 toULongLong (bool *ok=nullptr) const
 
QUrl toUrl () const
 
QUuid toUuid () const
 
Type type () const
 
QString typeName () const
 
uint userType () const
 
template<typename Requested >
Requested value () const
 

Static Public Methods

static QVariant fromValue (const QVariant &value)
 
template<typename T >
static QVariant fromValue (const T &value)
 
static uint nameToType (const QString &name)
 
static QString typeToName (uint typeId)
 
template<typename T >
static uint typeToTypeId ()
 

Related Functions

These are not member functions

#define CS_DECLARE_METATYPE(TYPE)
 
bool operator!= (const QVariant &v1, const QVariant &v2)
 
QDataStreamoperator<< (QDataStream &stream, QVariant &data)
 
bool operator== (const QVariant &v1, const QVariant &v2)
 
QDataStreamoperator>> (QDataStream &stream, const QVariant &data)
 
qvariant_cast (const QVariant &x)
 
 QVariantHash
 
 QVariantList
 
 QVariantMap
 

Detailed Description

The QVariant class is used to store a single value of some other data type. Not every value can be stored in a QVariant using every data type. For example, a QString can be retrieved as QKeySequence but it can not be retrieved as a QPoint. The QObject::property() system relies on the enums, methods, and data storage which is provided by this class.

The data stored in a QVariant has a corresponding integer type id which is defined in the QVariant::Type enum. If there is no match then the integer type id is represented by the value QVariant::Type::UserType.

The QVariant::Type enum contains a list of the CsCore and CsGui data types most often associated with variants. QVariant can be extended to support other data types by calling the CS_DECLARE_METATYPE() macro.

An important feature of the property system is the ability to provide a string representation for a given data type. For example, given the data type QStringList the string representation would be "QStringList" and for a bool the string representation would be "bool". To retrieve this string call typeName() on an existing QVariant object.

To find the integer type id which corresponds to a given data type name use the method nameToType().

Containers

There are five QVariant types which can be used to represent a container of QVariants. This allows storing complex data structures of arbitrary types. Although this is a useful feature and very convenient, it may be less efficient than storing the data in a standard strongly typed container.

The "to" Methods

There are a variety of methods which begin with the name "to" and are used to convert a variant value to the requested type. For example toString(), toDate(), or toInt() are methods to convert the variant to a QString, QDate, and an integer respectively. The returned value will be a copy even if no conversion occurs. If the conversion fails the return value will be an empty, null, or zero based on the requested type.

The following example shows several ways to use a QVariant.

QDataStream out(...);
QVariant v(123); // 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"); // variant now contains a QByteArray
v = QVariant(tr("hello")); // variant now contains a QString
int y = v.toInt(); // y = 0 since v can not 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", v.typeName()); // prints "Type is int"
v = v.toInt() + 100; // variant now holds the value 223

GUI Usage

QVariant is part of the CsCore library and can not provide conversion functions to data types defined in another library such as CsGui. This means you will not find a toColor() method to convert a QVariant value to a QColor.

The correct way to accomplish this is by using the templated value() method.

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

Converting from a QColor to a QVariant is automatic for data types which are supported by QVariant.

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

Using canConvert() and convert()

To find out what data type the QVariant holds use the type() method and to convert the object to a different type use convert().

To verify if a given object can be converted to a different type, use canConvert(). It is possible for canConvert() to return true, but then convert() will return false. This happens because canConvert() only looks at the source and target data types, not the actual values.

For example, canConvert(Type::Int) will return true when called on a variant which contains a string. Passing this enum asks if the string can be converted to an int, which is valid. However, if the string contains non-numeric characters it can not be converted to an integer, so convert() will fail.

Member Enumeration Documentation

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

Constant Description
QVariant::Invalid Invalid data type
QVariant::BitArray QBitArray
QVariant::Bitmap QBitmap
QVariant::Bool bool
QVariant::Brush QBrush
QVariant::ByteArray QByteArray
QVariant::Char QChar
QVariant::Char32 QChar32
QVariant::Color QColor
QVariant::Cursor QCursor
QVariant::Date QDate
QVariant::DateTime QDateTime
QVariant::Double double
QVariant::EasingCurve QEasingCurve
QVariant::Font QFont
QVariant::Hash QVariantHash
QVariant::Icon QIcon
QVariant::Image QImage
QVariant::Int int
QVariant::KeySequence QKeySequence
QVariant::Line QLine
QVariant::LineF QLineF
QVariant::List QVariantList
QVariant::Locale QLocale
QVariant::LongLong long long
QVariant::Map QVariantMap
QVariant::Matrix QMatrix
QVariant::Transform QTransform
QVariant::Matrix4x4 QMatrix4x4
QVariant::Palette QPalette
QVariant::Pen QPen
QVariant::Pixmap QPixmap
QVariant::Point QPoint
QVariant::PointF QPointF
QVariant::Polygon QPolygon
QVariant::Quaternion QQuaternion
QVariant::Rect QRect
QVariant::RectF QRectF
QVariant::RegularExpression QRegularExpression
QVariant::Region QRegion
QVariant::Size QSize
QVariant::SizeF QSizeF
QVariant::SizePolicy QSizePolicy
QVariant::String QString
QVariant::String8 QString8
QVariant::String16 QString16
QVariant::StringList QStringList
QVariant::TextFormat QTextFormat
QVariant::TextLength QTextLength
QVariant::Time QTime
QVariant::UInt uint
QVariant::ULongLong quint64
QVariant::Url QUrl
QVariant::Vector2D</td QVector2D
QVariant::Vector3D QVector3D
QVariant::Vector4D QVector4D
QVariant::UserType Base value for user defined types

Constructor & Destructor Documentation

QVariant::QVariant ( )
inline

Constructs an empty QVariant.

QVariant::QVariant ( Type  type)

Constructs a empty variant with the data type of type.

QVariant::QVariant ( uint  typeId,
const void *  copy 
)

Constructs a variant with of a type of typeId and initializes with copy, if the copy is not a null pointer. Ensure you pass the address of the variable you want stored.

This constructor in seldom used. The QVariant::fromValue() method should be used instead to construct variants from the pointer types represented by VoidStar, QObjectStar and QWidgetStar.

See also
QVariant::fromValue(), Type
QVariant::QVariant ( const QVariant &  other)

Copy constructs a new QVariant from other.

QVariant::QVariant ( QVariant &&  other)
default

Move constructs a new QVariant from other.

QVariant::QVariant ( bool  value)

Constructs a new variant with a boolean of value.

QVariant::QVariant ( int  value)

Constructs a new variant with an integer of value.

QVariant::QVariant ( uint  value)

Constructs a new variant with an unsigned integer of value.

QVariant::QVariant ( qint64  value)

Constructs a new variant with a qint64 integer of value.

QVariant::QVariant ( quint64  value)

Constructs a new variant with a quint64 integer of value.

QVariant::QVariant ( double  value)

Constructs a new variant with a floating point of value.

QVariant::QVariant ( float  value)

Constructs a new variant with a floating point of value.

QVariant::QVariant ( QChar32  value)

Constructs a new variant with a QChar of value.

QVariant::QVariant ( QString  value)

Constructs a new variant with a QString of value.

QVariant::QVariant ( QByteArray  value)

Constructs a new variant with a QByteArray of value.

QVariant::QVariant ( QBitArray  value)

Constructs a new variant with a QBitArray of value.

QVariant::QVariant ( QString16  value)

Constructs a new variant with a QString16 of value.

QVariant::QVariant ( QStringList  value)

Constructs a new variant with a QStringList of value.

QVariant::QVariant ( QRegularExpression8  value)

Constructs a new variant with a QRegularExpression of value.

QVariant::QVariant ( QDate  value)

Constructs a new variant with a QDate of value.

QVariant::QVariant ( QTime  value)

Constructs a new variant with a QTime of value.

QVariant::QVariant ( QDateTime  value)

Constructs a new variant with a QDateTime of value.

QVariant::QVariant ( QLocale  value)

Constructs a new variant with a QLocale of value.

QVariant::QVariant ( QList< QVariant >  value)

Constructs a new variant with a list of QVariants contained of value.

QVariant::QVariant ( QHash< QString, QVariant >  value)

Constructs a new variant with a hash of QVariants contained of value.

QVariant::QVariant ( QMap< QString, QVariant >  value)

Constructs a new variant with a map of QVariants contained of value.

QVariant::QVariant ( QMultiHash< QString, QVariant >  value)

Constructs a new variant with a multi hash of value.

QVariant::QVariant ( QMultiMap< QString, QVariant >  value)

Constructs a new variant with a multi map of value.

QVariant::QVariant ( QJsonValue  value)

Constructs a new variant with a QJsonValue of value.

QVariant::QVariant ( QJsonObject  value)

Constructs a new variant with a QJsonObject of value.

QVariant::QVariant ( QJsonArray  value)

Constructs a new variant with a QJsonArray of value.

QVariant::QVariant ( QJsonDocument  value)

Constructs a new variant with a QJsonDocument of value.

QVariant::QVariant ( QRect  value)

Constructs a new variant with a QRect of value.

QVariant::QVariant ( QRectF  value)

Constructs a new variant with a QRectF of value.

QVariant::QVariant ( QSize  value)

Constructs a new variant with a QSize of value.

QVariant::QVariant ( QSizeF  value)

Constructs a new variant with a QSizeF of value.

QVariant::QVariant ( QLine  value)

Constructs a new variant with a QLine of value.

QVariant::QVariant ( QLineF  value)

Constructs a new variant with a QLineF of value.

QVariant::QVariant ( QPoint  value)

Constructs a new variant with a QPoint of value.

QVariant::QVariant ( QPointF  value)

Constructs a new variant with a QPointF of value.

QVariant::QVariant ( QEasingCurve  value)

Constructs a new variant with an QEasingCurve of value.

QVariant::QVariant ( QModelIndex  value)

Constructs a new variant with a QModelIndex of value.

QVariant::QVariant ( QPersistentModelIndex  value)

Constructs a new variant with a QPersistentModelIndex of value.

QVariant::QVariant ( QUuid  value)

Constructs a new variant with a QUuid of value.

QVariant::QVariant ( QUrl  value)

Constructs a new variant with a QUrl of value.

QVariant::QVariant ( QDataStream stream)

Reads the variant from the data given stream.

QVariant::~QVariant ( )
default

Destroys the QVariant and the contained object.

Method Documentation

template<typename T >
bool QVariant::canConvert ( ) const
inline

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

QVariant v = 42;
v.canConvert<int>(); // returns true
v.canConvert<QString>(); // returns true
MyCustomStruct s;
v.setValue(s);
v.canConvert<int>(); // returns false
v.canConvert<MyCustomStruct>(); // returns true
See also
convert()
bool QVariant::canConvert ( uint  newType) const

Returns true if the current variant type can be cast to the requested variant newType. It is possible for this method to return true, but then convert() will return false. This happens because this method only looks at the source and target data types, not the actual values.

Current TypeRequested Type
Bitmap Image, Pixmap
Bool ByteArray, Char, Double, Float, Int, Long, LongLong, QChar, SChar, Short, String, String16, UChar, UInt, ULong, ULongLong, UShort
Brush Color, Pixmap
ByteArray Bool, Char, Color, Double, Float, Int, Long, LongLong, QChar, SChar, Short, String, String16, UChar, UInt, ULong, ULongLong, UShort
Char Bool, ByteArray, Int, Long, LongLong, QChar, Short, SChar, UChar, UInt, ULong, ULongLong, UShort
QChar Bool, ByteArray, Char, Int, Long, LongLong, Short, SChar, String, String16, UChar, UInt, LongLong, ULong, ULongLong, UShort
SChar Bool, ByteArray, Char, Int, Long, LongLong, QChar, Short, UChar, UInt, ULong, ULongLong, UShort
UChars Bool, ByteArray, Char, Int, Long, LongLong, QChar, SChar, Short, UInt, ULong, ULongLong, UShort
Color Brush, ByteArray, String, String16
Date DateTime, String, String16
DateTime Date, String, String16, Time
Double Bool, ByteArray, Char, Float, Int, Long, LongLong, QChar, SChar, Short, String, String16, UChar, UInt, ULong, ULongLong, UShort
Float Bool, ByteArray, Char, Double, Int, Long, LongLong, QChar, SChar, Short, String, String16, UChar, UInt, ULong, ULongLong, UShort
Font String, String16
Image Bitmap, Pixmap
Hash Map
Int Bool, ByteArray, Char, Double, Float, Long, LongLong, QChar, SChar, Short, String, String16, UChar, UInt, ULong, ULongLong, UShort
JsonArray List
JsonObject Hash, Map
JsonValue Bool, Char, Double, Float, Hash, Int, Long, LongLong, List, Map, QChar, Short, SChar, String, String16, UChar, UInt, ULong, ULongLong, UShort
KeySequence String, String16
Line LineF
LineF Line
List StringList
Note: Conversion is only valid when the items in the List can be converted to Strings
Long Bool, ByteArray, Char, Double, Float, Int, LongLong, QChar, SChar, Short, String, String16, UChar, UInt, ULong, ULongLong, UShort
LongLong Bool, ByteArray, Char, Double, Float, Int, Long, QChar, SChar, Short, String, String16, UChar, UInt, ULong, ULongLong, UShort
Map Hash
ModelIndex PersistentModelIndex
PersistentModelIndex ModelIndex
Pixmap Bitmap, Brush, Image
Point PointF
PointF Point
Rect RectF
RectF Rect
Short Bool, ByteArray, Char, Double, Float, Int, Long, LongLong, QChar, SChar, String, String16, UChar, UInt, ULong, ULongLong, UShort
Size SizeF
SizeF Size
String, String16 Bool, ByteArray, Char, Color, Date, DateTime, Double, Float, Font, Int, KeySequence, Long, LongLong, QChar, SChar, Short, StringList, Time, UChar, UInt, ULong, ULongLong, Url, UShort, Uuid
StringList List, String, String16
Note: Conversions to Strings are only valid when the StringList contains exactly one item
Time String, String16
UInt Bool, ByteArray, Char, Double, Float, Int, Long, LongLong, QChar, SChar, Short, String, String16, UChar, ULong, ULongLong, UShort
ULong Bool, ByteArray, Char, Double, Float, Int, Long, LongLong, QChar, SChar, Short, String, String16, UChar, UInt, ULongLong, UShort
ULongLong Bool, ByteArray, Char, Double, Float, Int, Long, LongLong, QChar, SChar, Short, String, String16, UChar, UInt, ULong, UShort
UShort Bool, ByteArray, Char, Double, Float, Int, Long, LongLong, QChar, SChar, Short, String, String16, UChar, UInt, ULong, ULongLong
Url ByteArray, String, String16
Uuid ByteArray, String, String16
See also
convert()
void QVariant::clear ( )

Sets this variant to an invalid data type and the integer type id to zero, which is the enum value QVariant::Invalid.

template<typename T >
bool QVariant::convert ( )
inline

Casts the variant to the requested type T. If the conversion can not be done the variant is set to an invalid data type and the enum type is set to QVariant::Invalid.

Returns true if the variant was successfully converted, otherwise returns false.

See also
canConvert(), clear()
bool QVariant::convert ( uint  newType)

Casts the variant to the requested type newType. If the conversion can not be done the variant is set to an invalid data type and the enum type is set to QVariant::Invalid.

Returns true if the variant was successfully converted, otherwise returns false.

See also
canConvert(), clear()
int64_t QVariant::enumToInt ( ) const

If the current QVariant contains a value which is an enum or a flag this method will return the value as a signed integer. Otherwise returns the value 0.

uint64_t QVariant::enumToUInt ( ) const

If the current QVariant contains a value which is an enum or a flag this method will return the value as an unsigned integer. Otherwise returns the value 0.

QVariant QVariant::fromValue ( const QVariant &  value)
inlinestatic

Returns a QVariant containing a copy of value. This is the same as calling setValue() on a new QVariant.

See also
setValue(), value()
template<typename T >
QVariant QVariant::fromValue ( const T &  value)
inlinestatic

Returns a QVariant containing a copy of value. This is the same as calling setValue() on a new QVariant.

MyCustomStruct s;
Note
If you are working with custom types use the CS_DECLARE_METATYPE() macro to register the custom type.
See also
setValue(), value()
template<typename T >
T QVariant::getData ( ) const

Returns the value for this QVariant if the data type of the QVariant matches T. Otherwise returns a default constructed T.

See also
getDataOr()
template<typename T >
std::optional< T > QVariant::getDataOr ( ) const

Returns the value in this QVariant if the type is T. Otherwise this method returns an empty std::optional of type T.

Note
Using the return type of std::optional provides a clean way to indicate the requested data type T was not stored in this variant. A caller of this method can test the optional by calling has_value().
QVariant var = someMethod();
std::optional<QString> result = var.getDataOr<QString>();
if (result.has_value()) {
return result.value().toLower();
} else {
return QString("Variant did not contain a string");
}
See also
getData()
bool QVariant::isEnum ( ) const

Returns true if the current QVariant contains a value which is an enum or a flag.

bool QVariant::isValid ( ) const
inline

Returns true if the data type in this variant is not QVariant::Invalid, otherwise returns false.

std::optional< QVariant > QVariant::maybeConvert ( uint  requested_type) const

Returns an std::optional containing the value of this QVariant converted to the requested_type or an empty optional if the conversion fails.

Note
Using the return type of std::optional provides a clean way to indicate the requested data type T was not stored in this variant. A caller of this method can test the optional by calling has_value().
uint QVariant::nameToType ( const QString name)
static

Converts the data type specified by name, to the corresponding integer type id. If the name can not be converted the return value is zero.

See also
typeToName()
bool QVariant::operator!= ( const QVariant &  other) const
inline

Compares this QVariant with other and returns true if they are not equal, otherwise returns false.

QVariant & QVariant::operator= ( const QVariant &  other)

Copy assigns from other and returns a reference to this object.

QVariant & QVariant::operator= ( QVariant &&  other)
inline

Move assigns from other and returns a reference to this object.

bool QVariant::operator== ( const QVariant &  other) const
inline

Compares this QVariant with other and returns true if they are equal, otherwise returns false. In the case of custom types, their equality operators are not called. Instead their addresses are compared.

void QVariant::setValue ( const QVariant &  value)
inline

Copies the data in value into this QVariant.

See also
value(), fromValue(), canConvert()
template<typename T >
void QVariant::setValue ( const T &  value)

Copies value into this QVariant.

v.setValue(5);
int i = v.toInt(); // i is now 5
QString s = v.toString() // s is now "5"
MyCustomStruct c;
v.setValue(c);
MyCustomStruct c2 = v.value<MyCustomStruct>();
See also
value(), fromValue(), canConvert()
void QVariant::swap ( QVariant &  other)
inline

Swaps variant other with this variant. This operation is very fast and never fails.

QBitArray QVariant::toBitArray ( ) const

Returns the variant as a QBitArray if the variant has a data type of BitArray, otherwise returns an empty QBitArray.

See also
canConvert(), convert()
bool QVariant::toBool ( bool *  ok = nullptr) const

Returns the variant as a bool if the variant has a data type of Bool.

Returns true if the variant has a data type of 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(), convert()
QByteArray QVariant::toByteArray ( ) const

Returns the variant as a QByteArray if the variant has a data type of ByteArray or String, otherwise returns an empty byte array.

See also
canConvert(), convert()
QChar32 QVariant::toChar ( ) const

Returns the variant as a QChar32 if the variant has a data type of QChar, Char, Int, or UInt. If the conversion fails it returns a null QChar32.

See also
canConvert(), convert()
QDate QVariant::toDate ( ) const

Returns the variant as a QDate if the variant has a data type of Date, DateTime, or String, otherwise returns an invalid date. If the type() is String, an invalid date will be returned if the string can not be parsed as a Qt::ISODate format date.

See also
canConvert(), convert()
QDateTime QVariant::toDateTime ( ) const

Returns the variant as a QDateTime if the variant has a data type of 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 can not be parsed as a Qt::ISODate format date/time.

See also
canConvert(), convert()
double QVariant::toDouble ( bool *  ok = nullptr) const

Returns the variant as a double if the variant has a data type of Double, Float, Bool, ByteArray, Int, LongLong, String, UInt, or ULongLong, otherwise returns 0.0. If ok is not a nullptr, this method will set the value according to the success of the conversion. The value is false to indicate the conversion failed, otherwise ok is set to true.

See also
canConvert(), convert()
QEasingCurve QVariant::toEasingCurve ( ) const

Returns the variant as a QEasingCurve if the variant has a data type of EasingCurve, otherwise returns a default easing curve.

See also
canConvert(), convert()
float QVariant::toFloat ( bool *  ok = nullptr) const

Returns the variant as a float if the variant has a data type of Double, Float, Bool, ByteArray, Int, LongLong, String, UInt, or ULongLong, otherwise returns 0.0. If ok is not a nullptr, this method will set the value according to the success of the conversion. The value is false to indicate the conversion failed, otherwise ok is set to true.

See also
canConvert(), convert()
QHash< QString, QVariant > QVariant::toHash ( ) const

Returns the variant as a QHash<QString, QVariant> if the variant has a data type of Hash, otherwise returns an empty QHash.

See also
canConvert(), convert()
int QVariant::toInt ( bool *  ok = nullptr) const

Returns the variant as an int if the variant has a data type of Int, Bool, ByteArray, Char, Double, LongLong, String, UInt, or ULongLong, otherwise return a value of 0. If ok is not a nullptr, this method will set the value according to the success of the conversion. The value is false to indicate the conversion failed, otherwise ok is set to true.

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 QString8::toInteger().
See also
canConvert(), convert()
QJsonArray QVariant::toJsonArray ( ) const

Returns the variant as a QJsonArray if the variant has a data type of JsonArray, otherwise returns a default constructed QJsonArray.

See also
canConvert(), convert()
QJsonDocument QVariant::toJsonDocument ( ) const

Returns the variant as a QJsonDocument if the variant has a data type of JsonDocument, otherwise returns a default constructed QJsonDocument.

See also
canConvert(), convert()
QJsonObject QVariant::toJsonObject ( ) const

Returns the variant as a QJsonObject if the variant has a data type of JsonObject, otherwise returns a default constructed QJsonObject.

See also
canConvert(), convert()
QJsonValue QVariant::toJsonValue ( ) const

Returns the variant as a QJsonValue if the variant has a data type of JsonValue, otherwise returns a default constructed QJsonValue.

See also
canConvert(), convert()
QLine QVariant::toLine ( ) const

Returns the variant as a QLine if the variant has a data type of Line, otherwise returns an invalid QLine.

See also
canConvert(), convert()
QLineF QVariant::toLineF ( ) const

Returns the variant as a QLineF if the variant has a data type of LineF, otherwise returns an invalid QLineF.

See also
canConvert(), convert()
QList< QVariant > QVariant::toList ( ) const

Returns the variant as a QVariantList if the variant has a data type of List or StringList, otherwise returns an empty list.

See also
canConvert(), convert()
QLocale QVariant::toLocale ( ) const

Returns the variant as a QLocale if the variant has a data type of Locale, otherwise returns an invalid QLocale.

See also
canConvert(), convert()
qint64 QVariant::toLongLong ( bool *  ok = nullptr) const

Returns the variant as a qint64 integer if the variant has a data type of LongLong, Bool, ByteArray, Char, Double, Int, String, UInt, or ULongLong, otherwise returns 0. If ok is not a nullptr, this method will set the value according to the success of the conversion. The value is false to indicate the conversion failed, otherwise ok is set to true.

See also
canConvert(), convert()
QMap< QString, QVariant > QVariant::toMap ( ) const

Returns the variant as a QMap<QString, QVariant> if the variant has a data type of Map, otherwise returns an empty QMap.

See also
canConvert(), convert()
QModelIndex QVariant::toModelIndex ( ) const

Returns the variant as a QModelIndex if the variant has a data type of ModelIndex, otherwise returns a default constructed QModelIndex.

See also
canConvert(), convert()
QMultiHash< QString, QVariant > QVariant::toMultiHash ( ) const

Returns the variant as a QMultiHash<QString, QVariant> if the variant has a data type of MultiHash, otherwise returns an empty multi QMultiHash.

See also
canConvert(), convert()
QMultiMap< QString, QVariant > QVariant::toMultiMap ( ) const

Returns the variant as a QMultiMap<QString, QVariant> if the variant has a data type of MultiMap, otherwise returns an empty multi QMultiMap.

See also
canConvert(), convert()
QPersistentModelIndex QVariant::toPersistentModelIndex ( ) const

Returns the variant as a QPersistentModelIndex if the variant has a data type of PersistentModelIndex, otherwise returns a default constructed QPersistentModelIndex.

See also
canConvert(), convert(), toPersistentModelIndex()
QPoint QVariant::toPoint ( ) const

Returns the variant as a QPoint if the variant has a data type of Point or PointF, otherwise returns a null QPoint.

See also
canConvert(), convert()
QPointF QVariant::toPointF ( ) const

Returns the variant as a QPointF if the variant has a data type of Point or PointF, otherwise returns a null QPointF.

See also
canConvert(), convert()
qreal QVariant::toReal ( bool *  ok = nullptr) const

Returns the variant as a qreal if the variant has a data type of Double, Float, Bool, ByteArray, Int, LongLong, String, UInt, or ULongLong, otherwise returns 0.0. If ok is not a nullptr, this method will set the value according to the success of the conversion. The value is false to indicate the conversion failed, otherwise ok is set to true.

See also
canConvert(), convert()
QRect QVariant::toRect ( ) const

Returns the variant as a QRect if the variant has a data type of Rect, otherwise returns an invalid QRect.

See also
canConvert(), convert()
QRectF QVariant::toRectF ( ) const

Returns the variant as a QRectF if the variant has a data type of Rect or RectF, otherwise returns an invalid QRectF.

See also
canConvert(), convert()
QRegularExpression8 QVariant::toRegularExpression ( ) const

Returns the variant as a QRegularExpression8 if the variant has a data type of RegularExpression otherwise returns an empty QRegularExpression8.

See also
canConvert(), convert()
QSize QVariant::toSize ( ) const

Returns the variant as a QSize if the variant has a data type of Size, otherwise returns an invalid QSize.

See also
canConvert(), convert()
QSizeF QVariant::toSizeF ( ) const

Returns the variant as a QSizeF if the variant has a data type of SizeF, otherwise returns an invalid QSizeF.

See also
canConvert(), convert()
QString8 QVariant::toString ( ) const

Returns the variant as a QString8 if the variant has a data type of String, Bool, ByteArray, Char, QDate, DateTime, Double, Int, LongLong, StringList, Time, UInt, or ULongLong, otherwise returns an empty string.

See also
canConvert(), convert()
QString16 QVariant::toString16 ( ) const

Returns the variant as a QString16 if the variant has a data type of String, Bool, ByteArray, Char, Date, DateTime, Double, Int, LongLong, QStringList, Time, UInt, or ULongLong, otherwise returns an empty string.

See also
canConvert(), convert()
QStringList QVariant::toStringList ( ) const

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

See also
canConvert(), convert()
QTime QVariant::toTime ( ) const

Returns the variant as a QTime if the variant has a data type of Time, DateTime, or String, otherwise returns an invalid time. If the type() is QString, an invalid time will be returned if the string can not be parsed as a Qt::ISODate format time.

See also
canConvert(), convert()
uint QVariant::toUInt ( bool *  ok = nullptr) const

Returns the variant as an unsigned integer if the variant has a data type of UInt, Bool, ByteArray, Char, Double, Int, LongLong, String, or ULongLong, otherwise returns 0. If ok is not a nullptr, this method will set the value according to the success of the conversion. The value is false to indicate the conversion failed, otherwise ok is set to true.

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().
See also
canConvert(), convert()
quint64 QVariant::toULongLong ( bool *  ok = nullptr) const

Returns the variant as an quint64 integer if the variant has a data type of ULongLong, Bool, ByteArray, Char, Double, Int, LongLong, String, or UInt, otherwise returns 0. If ok is not a nullptr, this method will set the value according to the success of the conversion. The value is false to indicate the conversion failed, otherwise ok is set to true.

See also
canConvert(), convert()
QUrl QVariant::toUrl ( ) const

Returns the variant as a QUrl if the variant has a data type of Url, otherwise returns an invalid QUrl.

See also
canConvert(), convert()
QUuid QVariant::toUuid ( ) const

Returns the variant as a QUuid if the variant has a data type of Uuid or String, otherwise returns a default constructed QUuid.

See also
canConvert(), convert()
Type QVariant::type ( ) const

Returns the enum value corresponding to the data type which is currently stored in this variant. Returns QVariant::UserType if the data type is not one of the pre-defined types.

See also
userType()
QString QVariant::typeName ( ) const

Returns the name of the data type or class stored in the variant. For example, for the class QFont this method will return "QFont". For primitive types like bool this method will return "bool". If the variant is empty returns an empty string.

For user defined data types this method will return the name which was previously registered when CS_DECLARE_METATYPE() was called.

QString QVariant::typeToName ( uint  typeId)
static

Converts the given integer type id typeId to the name of the corresponding data type. If the typeId is QVariant::Invalid this method returns an empty string.

See also
nameToType()
template<typename T >
uint QVariant::typeToTypeId ( )
inlinestatic

Returns the integer type id corresponding to the template parameter T.

uint QVariant::userType ( ) const

Returns the integer type id for this variant. For non-user defined values, which are listed in the QVariant::Type enum, this method returns the same result as calling the type() method.

See also
type()
template<typename Requested >
Requested QVariant::value ( ) const

Returns the value in this variant if it can be converted to the data type specified in the template parameter Requested.

Calling canConvert() will indicate whether the current type can be converted to a particular Requested type. If the value can not be converted, a default constructed value of type Requested will be returned.

QVariant data = 42;
// example 1
int cnt = data.value<int>(); // same as calling data.toInt()
// example 2
QString str = data.value<QString>(); // same as calling data.toString(), str is now "42"
// example 3
// conConvert() will return false since 42 can not be converted to MyCustomStruct
MyCustomStruct structA;
if (data.canConvert<MyCustomStruct>()) {
structA = data.value<MyCustomStruct>();
}
// example 4
// following call to value() will fail, structB will contain a default constructed value
MyCustomStruct structB = data.value<MyCustomStruct>();
See also
setValue(), fromValue(), canConvert()

Friends And Related Function Documentation

#define CS_DECLARE_METATYPE
related

This macro makes the Type known to the Variant system. The Type being registered must have a public default constructor, a public copy constructor and a public destructor. It is recommended the Type should have and support an operator==() method.

This macro should be placed after the declaration of the class or struct, which is usually in the header file.

The following is an example of a typical use case.

class MyNewClass {
int i;
};

If MyNewClass is located within a namespace the CS_DECLARE_METATYPE() macro needs to appear outside the namespace.

namespace MyProject {
// ...
}
CS_DECLARE_METATYPE(MyProject::MyNewClass)

The following is an example of how to use MyNewClass in a QVariant.

MyNewClass obj;
QVariant data;
data.setValue(obj); // copy obj into a variant
// ...
// retrieve the value
MyNewClass newObj = data.value<MyNewClass>();
bool operator!= ( const QVariant &  v1,
const QVariant &  v2 
)
related

Returns false if v1 and v2 are equal, otherwise returns true.

QDataStream & operator<< ( QDataStream stream,
QVariant &  data 
)
related

Writes the given data to the stream. Returns a reference to the stream.

See also
operator>>()
bool operator== ( const QVariant &  v1,
const QVariant &  v2 
)
related

Returns true if v1 and v2 are equal, otherwise returns false.

QDataStream & operator>> ( QDataStream stream,
const QVariant &  data 
)
related

Reads from the stream into the given data. Returns a reference to the stream.

See also
operator<<()
T qvariant_cast< T > ( const QVariant &  x)
related
Deprecated:
Use x.value<T>() instead.
See also
QVariant::value()
QVariantHash ( )
related

Equivalent to QHash<QString, QVariant>.

QVariantList ( )
related

Equivalent to QList<QVariant>.

QVariantMap ( )
related

Equivalent to QMap<QString, QVariant>>.