Member Function Documentation
QVariant::QVariant()
Constructs an invalid variant.
QVariant::QVariant(const QRegExp & regExp)
Constructs a new variant with the regexp value regExp.
Constructs a new variant with the regular expression value re.
This function was introduced in Qt 5.0.
QVariant::QVariant(const QUrl & val)
Constructs a new variant with a url value of val.
QVariant::QVariant(const QEasingCurve & val)
Constructs a new variant with an easing curve value, val.
This function was introduced in Qt 4.7.
QVariant::QVariant(Type type)
Constructs a null variant of type type.
QVariant::QVariant(int typeId, const void * copy)
Constructs variant of type typeId, 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 QVariant::fromValue() instead to construct variants from the pointer types represented by QMetaType::VoidStar, QMetaType::QObjectStar and QMetaType::QWidgetStar.
See also QVariant::fromValue() and Type(obsolete).
QVariant::QVariant(const QVariant & p)
Constructs a copy of the variant, p, passed as the argument to this constructor.
QVariant::QVariant(QDataStream & s)
Reads the variant from the data stream, s.
QVariant::QVariant(int val)
Constructs a new variant with an integer value, val.
QVariant::QVariant(uint val)
Constructs a new variant with an unsigned integer value, val.
QVariant::QVariant(qlonglong val)
Constructs a new variant with a long long integer value, val.
QVariant::QVariant(qulonglong val)
Constructs a new variant with an unsigned long long integer value, val.
QVariant::QVariant(bool val)
Constructs a new variant with a boolean value, val.
QVariant::QVariant(double val)
Constructs a new variant with a floating point value, val.
QVariant::QVariant(float val)
Constructs a new variant with a floating point value, val.
This function was introduced in Qt 4.6.
QVariant::QVariant(const char * val)
Constructs a new variant with a string value of val. The variant creates a deep copy of val into a QString assuming UTF-8 encoding on the input val.
Note that val is converted to a QString for storing in the variant and QVariant::userType() will return QMetaType::QString for the variant.
You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications.
QVariant::QVariant(const QByteArray & val)
Constructs a new variant with a bytearray value, val.
QVariant::QVariant(const QBitArray & val)
Constructs a new variant with a bitarray value, val.
QVariant::QVariant(const QString & val)
Constructs a new variant with a string value, val.
Constructs a new variant with a string value, val.
QVariant::QVariant(const QStringList & val)
Constructs a new variant with a string list value, val.
QVariant::QVariant(QChar c)
Constructs a new variant with a char value, c.
QVariant::QVariant(const QDate & val)
Constructs a new variant with a date value, val.
QVariant::QVariant(const QTime & val)
Constructs a new variant with a time value, val.
QVariant::QVariant(const QDateTime & val)
Constructs a new variant with a date/time value, val.
QVariant::QVariant(const QList<QVariant> & val)
Constructs a new variant with a list value, val.
QVariant::QVariant(const QMap<QString, QVariant> & val)
Constructs a new variant with a map of QVariants, val.
QVariant::QVariant(const QHash<QString, QVariant> & val)
Constructs a new variant with a hash of QVariants, val.
QVariant::QVariant(const QSize & val)
Constructs a new variant with a size value of val.
QVariant::QVariant(const QSizeF & val)
Constructs a new variant with a size value of val.
QVariant::QVariant(const QPoint & val)
Constructs a new variant with a point value of val.
QVariant::QVariant(const QPointF & val)
Constructs a new variant with a point value of val.
QVariant::QVariant(const QLine & val)
Constructs a new variant with a line value of val.
QVariant::QVariant(const QLineF & val)
Constructs a new variant with a line value of val.
QVariant::QVariant(const QRect & val)
Constructs a new variant with a rect value of val.
QVariant::QVariant(const QRectF & val)
Constructs a new variant with a rect value of val.
QVariant::QVariant(const QLocale & l)
Constructs a new variant with a locale value, l.
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().
bool QVariant::canConvert(int targetTypeId) const
Returns true if the variant's type can be cast to the requested type, targetTypeId. Such casting is done automatically when calling the toInt(), toBool(), ... methods.
The following casts are done automatically:
Type | Automatically Cast To |
Bool(obsolete) | Char(obsolete), Double(obsolete), Int(obsolete), LongLong(obsolete), String(obsolete), UInt(obsolete), ULongLong(obsolete) |
ByteArray(obsolete) | Double(obsolete), Int(obsolete), LongLong(obsolete), String(obsolete), UInt(obsolete), ULongLong(obsolete) |
Char(obsolete) | Bool(obsolete), Int(obsolete), UInt(obsolete), LongLong(obsolete), ULongLong(obsolete) |
Color(obsolete) | String(obsolete) |
Date(obsolete) | DateTime(obsolete), String(obsolete) |
DateTime(obsolete) | Date(obsolete), String(obsolete), Time(obsolete) |
Double(obsolete) | Bool(obsolete), Int(obsolete), LongLong(obsolete), String(obsolete), UInt(obsolete), ULongLong(obsolete) |
Font(obsolete) | String(obsolete) |
Int(obsolete) | Bool(obsolete), Char(obsolete), Double(obsolete), LongLong(obsolete), String(obsolete), UInt(obsolete), ULongLong(obsolete) |
KeySequence(obsolete) | Int(obsolete), String(obsolete) |
List(obsolete) | StringList(obsolete) (if the list's items can be converted to strings) |
LongLong(obsolete) | Bool(obsolete), ByteArray(obsolete), Char(obsolete), Double(obsolete), Int(obsolete), String(obsolete), UInt(obsolete), ULongLong(obsolete) |
Point(obsolete) | PointF(obsolete) |
Rect(obsolete) | RectF(obsolete) |
String(obsolete) | Bool(obsolete), ByteArray(obsolete), Char(obsolete), Color(obsolete), Date(obsolete), DateTime(obsolete), Double(obsolete), Font(obsolete), Int(obsolete), KeySequence(obsolete), LongLong(obsolete), StringList(obsolete), Time(obsolete), UInt(obsolete), ULongLong(obsolete) |
StringList(obsolete) | List(obsolete), String(obsolete) (if the list contains exactly one item) |
Time(obsolete) | String(obsolete) |
UInt(obsolete) | Bool(obsolete), Char(obsolete), Double(obsolete), Int(obsolete), LongLong(obsolete), String(obsolete), ULongLong(obsolete) |
ULongLong(obsolete) | Bool(obsolete), Char(obsolete), Double(obsolete), Int(obsolete), LongLong(obsolete), String(obsolete), UInt(obsolete) |
See also convert().
bool QVariant::canConvert() const
Returns true if the variant can be converted to the template type T, otherwise false.
Example:
QVariant v = 42;
v.canConvert<int>();
v.canConvert<QString>();
MyCustomStruct s;
v.setValue(s);
v.canConvert<int>();
v.canConvert<MyCustomStruct>();
See also convert().
void QVariant::clear()
Convert this variant to type Invalid and free up any resources used.
bool QVariant::convert(int targetTypeId)
Casts the variant to the requested type, targetTypeId. 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().
const DataPtr & QVariant::data_ptr() const
QVariant QVariant::fromValue(const T & value) [static]
Returns a QVariant containing a copy of value. Behaves exactly like setValue() otherwise.
Example:
MyCustomStruct s;
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.
See also setValue() and value().
bool QVariant::isNull() const
Returns true if this is a NULL variant, false otherwise.
bool QVariant::isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid(obsolete); otherwise returns false.
Type QVariant::nameToType(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.
void QVariant::setValue(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:
QVariant v;
v.setValue(5);
int i = v.toInt();
QString s = v.toString()
MyCustomStruct c;
v.setValue(c);
...
MyCustomStruct c2 = v.value<MyCustomStruct>();
See also value(), fromValue(), and canConvert().
void QVariant::swap(QVariant & other)
Swaps variant other with this variant. This operation is very fast and never fails.
This function was introduced in Qt 4.8.
QBitArray QVariant::toBitArray() const
Returns the variant as a QBitArray if the variant has type() BitArray(obsolete); otherwise returns an empty bit array.
See also canConvert() and convert().
bool QVariant::toBool() const
Returns the variant as a bool if the variant has type() Bool.
Returns true if the variant has type() Bool(obsolete), Char(obsolete), Double(obsolete), Int(obsolete), LongLong(obsolete), UInt(obsolete), or ULongLong(obsolete) and the value is non-zero, or if the variant has type String(obsolete) or ByteArray(obsolete) and its lower-case content is not empty, "0" or "false"; otherwise returns false.
See also canConvert() and convert().
QByteArray QVariant::toByteArray() const
Returns the variant as a QByteArray if the variant has type() ByteArray(obsolete) or String(obsolete) (converted using QString::fromUtf8()); otherwise returns an empty byte array.
See also canConvert() and convert().
QChar QVariant::toChar() const
Returns the variant as a QChar if the variant has type() Char(obsolete), Int(obsolete), or UInt(obsolete); otherwise returns an invalid QChar.
See also canConvert() and convert().
QDate QVariant::toDate() const
Returns the variant as a QDate if the variant has type() Date(obsolete), DateTime(obsolete), or String(obsolete); otherwise returns an invalid date.
If the type() is String(obsolete), 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() const
Returns the variant as a QDateTime if the variant has type() DateTime(obsolete), Date(obsolete), or String(obsolete); otherwise returns an invalid date/time.
If the type() is String(obsolete), 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().
double QVariant::toDouble(bool * ok = 0) const
Returns the variant as a double if the variant has type() Double(obsolete), QMetaType::Float, Bool(obsolete), ByteArray(obsolete), Int(obsolete), LongLong(obsolete), String(obsolete), UInt(obsolete), or ULongLong(obsolete); 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().
QEasingCurve QVariant::toEasingCurve() const
Returns the variant as a QEasingCurve if the variant has type() EasingCurve(obsolete); otherwise returns a default easing curve.
This function was introduced in Qt 4.7.
See also canConvert() and convert().
float QVariant::toFloat(bool * ok = 0) const
Returns the variant as a float if the variant has type() Double(obsolete), QMetaType::Float, Bool(obsolete), ByteArray(obsolete), Int(obsolete), LongLong(obsolete), String(obsolete), UInt(obsolete), or ULongLong(obsolete); 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.
This function was introduced in Qt 4.6.
See also canConvert() and convert().
QHash<QString, QVariant> QVariant::toHash() const
Returns the variant as a QHash<QString, QVariant> if the variant has type() Hash(obsolete); otherwise returns an empty map.
See also canConvert() and convert().
int QVariant::toInt(bool * ok = 0) const
Returns the variant as an int if the variant has type() Int(obsolete), Bool(obsolete), ByteArray(obsolete), Char(obsolete), Double(obsolete), LongLong(obsolete), String(obsolete), UInt(obsolete), or ULongLong(obsolete); 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(obsolete) 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().
See also canConvert() and convert().
QLine QVariant::toLine() const
Returns the variant as a QLine if the variant has type() Line(obsolete); otherwise returns an invalid QLine.
See also canConvert() and convert().
QLineF QVariant::toLineF() const
Returns the variant as a QLineF if the variant has type() LineF(obsolete); otherwise returns an invalid QLineF.
See also canConvert() and convert().
QList<QVariant> QVariant::toList() const
Returns the variant as a QVariantList if the variant has type() List(obsolete) or StringList(obsolete); otherwise returns an empty list.
See also canConvert() and convert().
QLocale QVariant::toLocale() const
Returns the variant as a QLocale if the variant has type() Locale(obsolete); otherwise returns an invalid QLocale.
See also canConvert() and convert().
qlonglong QVariant::toLongLong(bool * ok = 0) const
Returns the variant as a long long int if the variant has type() LongLong(obsolete), Bool(obsolete), ByteArray(obsolete), Char(obsolete), Double(obsolete), Int(obsolete), String(obsolete), UInt(obsolete), or ULongLong(obsolete); 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().
QMap<QString, QVariant> QVariant::toMap() const
Returns the variant as a QMap<QString, QVariant> if the variant has type() Map(obsolete); otherwise returns an empty map.
See also canConvert() and convert().
QPoint QVariant::toPoint() const
Returns the variant as a QPoint if the variant has type() Point(obsolete) or PointF(obsolete); otherwise returns a null QPoint.
See also canConvert() and convert().
QPointF QVariant::toPointF() const
Returns the variant as a QPointF if the variant has type() Point(obsolete) or PointF(obsolete); otherwise returns a null QPointF.
See also canConvert() and convert().
qreal QVariant::toReal(bool * ok = 0) const
Returns the variant as a qreal if the variant has type() Double(obsolete), QMetaType::Float, Bool(obsolete), ByteArray(obsolete), Int(obsolete), LongLong(obsolete), String(obsolete), UInt(obsolete), or ULongLong(obsolete); 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.
This function was introduced in Qt 4.6.
See also canConvert() and convert().
QRect QVariant::toRect() const
Returns the variant as a QRect if the variant has type() Rect(obsolete); otherwise returns an invalid QRect.
See also canConvert() and convert().
QRectF QVariant::toRectF() const
Returns the variant as a QRectF if the variant has type() Rect(obsolete) or RectF(obsolete); otherwise returns an invalid QRectF.
See also canConvert() and convert().
QRegExp QVariant::toRegExp() const
Returns the variant as a QRegExp if the variant has type() RegExp(obsolete); otherwise returns an empty QRegExp.
This function was introduced in Qt 4.1.
See also canConvert() and convert().
QRegularExpression QVariant::toRegularExpression() const
Returns the variant as a QRegularExpression if the variant has type() QRegularExpression; otherwise returns an empty QRegularExpression.
This function was introduced in Qt 5.0.
See also canConvert() and convert().
QSize QVariant::toSize() const
Returns the variant as a QSize if the variant has type() Size(obsolete); otherwise returns an invalid QSize.
See also canConvert() and convert().
QSizeF QVariant::toSizeF() const
Returns the variant as a QSizeF if the variant has type() SizeF(obsolete); otherwise returns an invalid QSizeF.
See also canConvert() and convert().
QString QVariant::toString() const
Returns the variant as a QString if the variant has type() String(obsolete), Bool(obsolete), ByteArray(obsolete), Char(obsolete), Date(obsolete), DateTime(obsolete), Double(obsolete), Int(obsolete), LongLong(obsolete), StringList(obsolete), Time(obsolete), UInt(obsolete), or ULongLong(obsolete); otherwise returns an empty string.
See also canConvert() and convert().
QStringList QVariant::toStringList() const
Returns the variant as a QStringList if the variant has type() StringList(obsolete), String(obsolete), or List(obsolete) of a type that can be converted to QString; otherwise returns an empty list.
See also canConvert() and convert().
QTime QVariant::toTime() const
Returns the variant as a QTime if the variant has type() Time(obsolete), DateTime(obsolete), or String(obsolete); otherwise returns an invalid time.
If the type() is String(obsolete), an invalid time will be returned if the string cannot be parsed as a Qt::ISODate format time.
See also canConvert() and convert().
uint QVariant::toUInt(bool * ok = 0) const
Returns the variant as an unsigned int if the variant has type() UInt(obsolete), Bool(obsolete), ByteArray(obsolete), Char(obsolete), Double(obsolete), Int(obsolete), LongLong(obsolete), String(obsolete), or ULongLong(obsolete); 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(obsolete) 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() and convert().
qulonglong QVariant::toULongLong(bool * ok = 0) const
Returns the variant as as an unsigned long long int if the variant has type() ULongLong(obsolete), Bool(obsolete), ByteArray(obsolete), Char(obsolete), Double(obsolete), Int(obsolete), LongLong(obsolete), String(obsolete), or UInt(obsolete); 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() const
Returns the variant as a QUrl if the variant has type() Url(obsolete); otherwise returns an invalid QUrl.
See also canConvert() and convert().
Type QVariant::type() const
Returns the storage type of the value stored in the variant. Although this function is declared as returning QVariant::Type(obsolete), the return value should be interpreted as QMetaType::Type. In particular, QVariant::UserType(obsolete) is returned here only if the value is equal or greater than QMetaType::User.
Note that return values in the ranges QVariant::Char(obsolete) through QVariant::RegExp(obsolete) and QVariant::Font(obsolete) through QVariant::Transform(obsolete) 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(obsolete), 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(obsolete).
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(obsolete), and they can be returned by this function. However, they are considered to be user defined types when tested against QVariant::Type(obsolete).
To test whether an instance of QVariant contains a data type that is compatible with the data type you are interested in, use canConvert().
const char * QVariant::typeName() const
Returns the name of the type stored in the variant. The returned strings describe the C++ datatype used to store the data: for example, "QFont", "QString", or "QVariantList". An Invalid variant returns 0.
const char * QVariant::typeToName(int typeId) [static]
Converts the int representation of the storage type, typeId, to its string representation.
Returns a null pointer if the type is QVariant::Invalid(obsolete) or doesn't exist.
int QVariant::userType() 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().
T QVariant::value() 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:
QVariant v;
MyCustomStruct c;
if (v.canConvert<MyCustomStruct>())
c = v.value<MyCustomStruct>();
v = 7;
int i = v.value<int>();
QString s = v.value<QString>();
MyCustomStruct c2 = v.value<MyCustomStruct>();
See also setValue(), fromValue(), and canConvert().
bool QVariant::operator!=(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().
QVariant & QVariant::operator=(const QVariant & variant)
Assigns the value of the variant variant to this variant.
bool QVariant::operator==(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.