![]() |
Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
|
\inmodule QtCore More...
#include <qvariant.h>
Classes | |
| struct | PrivateShared |
| struct | Private |
| class | Reference |
| The QVariant::Reference acts as a non-const reference to a QVariant. More... | |
| class | ConstReference |
| The QVariant::ConstReference acts as a const reference to a QVariant. More... | |
| class | ConstPointer |
| QVariant::ConstPointer is a template class that emulates a const pointer to QVariant. More... | |
| class | Pointer |
| QVariant::Pointer is a template class that emulates a non-const pointer to QVariant. More... | |
Public Types | |
| typedef Private | DataPtr |
Public Member Functions | |
| QVariant () noexcept | |
| Constructs an invalid variant. | |
| ~QVariant () | |
| Destroys the QVariant and the contained object. | |
| QVariant (QMetaType type, const void *copy=nullptr) | |
Constructs a variant of type type, and initializes it with a copy of {*copy} if copy is not \nullptr (in which case, copy must point to an object of type type). | |
| QVariant (const QVariant &other) | |
| Constructs a copy of the variant, p, passed as the argument to this constructor. | |
| template<typename T, typename... Args, if_constructible< T, Args... > = true> | |
| QVariant (std::in_place_type_t< T >, Args &&... args) noexcept(is_noexcept_constructible< q20::remove_cvref_t< T >, Args... >::value) | |
| template<typename T, typename U, typename... Args, if_constructible< T, std::initializer_list< U > &, Args... > = true> | |
| QVariant (std::in_place_type_t< T >, std::initializer_list< U > il, Args &&... args) noexcept(is_noexcept_constructible< q20::remove_cvref_t< T >, std::initializer_list< U > &, Args... >::value) | |
| QVariant (int i) noexcept | |
| Constructs a new variant with an integer value, val. | |
| QVariant (uint ui) noexcept | |
| Constructs a new variant with an unsigned integer value, val. | |
| QVariant (qlonglong ll) noexcept | |
| Constructs a new variant with a long long integer value, val. | |
| QVariant (qulonglong ull) noexcept | |
| Constructs a new variant with an unsigned long long integer value, val. | |
| QVariant (bool b) noexcept | |
| Constructs a new variant with a boolean value, val. | |
| QVariant (double d) noexcept | |
| Constructs a new variant with a floating point value, val. | |
| QVariant (float f) noexcept | |
| Constructs a new variant with a floating point value, val. | |
| QVariant (QChar qchar) noexcept | |
| Constructs a new variant with a char value, c. | |
| QVariant (QDate date) noexcept | |
| Constructs a new variant with a date value, val. | |
| QVariant (QTime time) noexcept | |
| Constructs a new variant with a time value, val. | |
| QVariant (const QBitArray &bitarray) noexcept | |
| Constructs a new variant with a bitarray value, val. | |
| QVariant (QBitArray &&bitarray) noexcept | |
| QVariant (const QByteArray &bytearray) noexcept | |
| Constructs a new variant with a bytearray value, val. | |
| QVariant (QByteArray &&bytearray) noexcept | |
| QVariant (const QDateTime &datetime) noexcept | |
| Constructs a new variant with a date/time value, val. | |
| QVariant (QDateTime &&datetime) noexcept | |
| QVariant (const QHash< QString, QVariant > &hash) noexcept | |
| Constructs a new variant with a hash of \l {QVariant}s, val. | |
| QVariant (QHash< QString, QVariant > &&hash) noexcept | |
| QVariant (const QJsonArray &jsonArray) noexcept | |
| QVariant (QJsonArray &&jsonArray) noexcept | |
| QVariant (const QJsonObject &jsonObject) noexcept | |
| QVariant (QJsonObject &&jsonObject) noexcept | |
| QVariant (const QList< QVariant > &list) noexcept | |
| Constructs a new variant with a list value, val. | |
| QVariant (QList< QVariant > &&list) noexcept | |
| QVariant (const QLocale &locale) noexcept | |
| Constructs a new variant with a locale value, l. | |
| QVariant (QLocale &&locale) noexcept | |
| QVariant (const QMap< QString, QVariant > &map) noexcept | |
| Constructs a new variant with a map of \l {QVariant}s, val. | |
| QVariant (QMap< QString, QVariant > &&map) noexcept | |
| QVariant (const QRegularExpression &re) noexcept | |
| QVariant (QRegularExpression &&re) noexcept | |
| QVariant (const QString &string) noexcept | |
| Constructs a new variant with a string value, val. | |
| QVariant (QString &&string) noexcept | |
| QVariant (const QStringList &stringlist) noexcept | |
| Constructs a new variant with a string list value, val. | |
| QVariant (QStringList &&stringlist) noexcept | |
| QVariant (const QUrl &url) noexcept | |
| Constructs a new variant with a url value of val. | |
| QVariant (QUrl &&url) noexcept | |
| QVariant (const QJsonValue &jsonValue) noexcept(Private::FitsInInternalSize< sizeof(CborValueStandIn)>) | |
| QVariant (QJsonValue &&jsonValue) noexcept(Private::FitsInInternalSize< sizeof(CborValueStandIn)>) | |
| QVariant (const QModelIndex &modelIndex) noexcept(Private::FitsInInternalSize< 8+2 *sizeof(quintptr)>) | |
| QVariant (QUuid uuid) noexcept(Private::FitsInInternalSize< 16 >) | |
| QVariant (QSize size) noexcept | |
| Constructs a new variant with a size value of val. | |
| QVariant (QSizeF size) noexcept(Private::FitsInInternalSize< sizeof(qreal) *2 >) | |
| Constructs a new variant with a size value of val. | |
| QVariant (QPoint pt) noexcept | |
| Constructs a new variant with a point value of val. | |
| QVariant (QPointF pt) noexcept(Private::FitsInInternalSize< sizeof(qreal) *2 >) | |
| Constructs a new variant with a point value of val. | |
| QVariant (QLine line) noexcept(Private::FitsInInternalSize< sizeof(int) *4 >) | |
| Constructs a new variant with a line value of val. | |
| QVariant (QLineF line) noexcept(Private::FitsInInternalSize< sizeof(qreal) *4 >) | |
| Constructs a new variant with a line value of val. | |
| QVariant (QRect rect) noexcept(Private::FitsInInternalSize< sizeof(int) *4 >) | |
| Constructs a new variant with a rect value of val. | |
| QVariant (QRectF rect) noexcept(Private::FitsInInternalSize< sizeof(qreal) *4 >) | |
| Constructs a new variant with a rect value of val. | |
| QVariant (const QEasingCurve &easing) noexcept(false) | |
| QVariant (QEasingCurve &&easing) noexcept | |
| QVariant (const QJsonDocument &jsonDocument) noexcept(false) | |
| QVariant (QJsonDocument &&jsonDocument) noexcept | |
| QVariant (const QPersistentModelIndex &modelIndex) noexcept(false) | |
| QVariant (QPersistentModelIndex &&modelIndex) noexcept | |
| QT_ASCII_CAST_WARN | QVariant (const char *str) noexcept(false) |
| Constructs a new variant with a string value of val. | |
| QVariant (QLatin1StringView string) noexcept(false) | |
| Constructs a new variant with a QString value from the Latin-1 string viewed by val. | |
| template<typename T, std::enable_if_t< std::disjunction_v< std::is_pointer< T >, std::is_member_pointer< T > >, bool > = false> | |
| QVariant (T)=delete | |
| QVariant & | operator= (const QVariant &other) |
| Move-assigns other to this QVariant instance. | |
| QVariant (QVariant &&other) noexcept | |
| Move-constructs a QVariant instance, making it point at the same object that other was pointing to. | |
| void | swap (QVariant &other) noexcept |
| int | userType () const |
| int | typeId () const |
| Returns the storage type of the value stored in the variant. | |
| const char * | typeName () const |
| Returns the name of the type stored in the variant. | |
| QMetaType | metaType () const |
| bool | canConvert (QMetaType targetType) const |
| bool | convert (QMetaType type) |
| Casts the variant to the requested type, targetType. | |
| bool | canView (QMetaType targetType) const |
| bool | isValid () const |
Returns true if the storage type of this variant is not QMetaType::UnknownType; otherwise returns false. | |
| bool | isNull () const |
Returns true if this is a null variant, false otherwise. | |
| void | clear () |
| Convert this variant to type QMetaType::UnknownType and free up any resources used. | |
| void | detach () |
| bool | isDetached () const |
| int | toInt (bool *ok=nullptr) const |
| Returns the variant as an int if the variant has userType() \l QMetaType::Int, \l QMetaType::Bool, \l QMetaType::QByteArray, \l QMetaType::QChar, \l QMetaType::Double, \l QMetaType::LongLong, \l QMetaType::QString, \l QMetaType::UInt, or \l QMetaType::ULongLong; otherwise returns 0. | |
| uint | toUInt (bool *ok=nullptr) const |
| Returns the variant as an unsigned int if the variant has userType() \l QMetaType::UInt, \l QMetaType::Bool, \l QMetaType::QByteArray, \l QMetaType::QChar, \l QMetaType::Double, \l QMetaType::Int, \l QMetaType::LongLong, \l QMetaType::QString, or \l QMetaType::ULongLong; otherwise returns 0. | |
| qlonglong | toLongLong (bool *ok=nullptr) const |
| Returns the variant as a long long int if the variant has userType() \l QMetaType::LongLong, \l QMetaType::Bool, \l QMetaType::QByteArray, \l QMetaType::QChar, \l QMetaType::Double, \l QMetaType::Int, \l QMetaType::QString, \l QMetaType::UInt, or \l QMetaType::ULongLong; otherwise returns 0. | |
| qulonglong | toULongLong (bool *ok=nullptr) const |
| Returns the variant as an unsigned long long int if the variant has metaType() \l QMetaType::ULongLong, \l QMetaType::Bool, \l QMetaType::QByteArray, \l QMetaType::QChar, \l QMetaType::Double, \l QMetaType::Int, \l QMetaType::LongLong, \l QMetaType::QString, or \l QMetaType::UInt; otherwise returns 0. | |
| bool | toBool () const |
| Returns the variant as a bool if the variant has userType() Bool. | |
| double | toDouble (bool *ok=nullptr) const |
| Returns the variant as a double if the variant has userType() \l QMetaType::Double, \l QMetaType::Float, \l QMetaType::Bool, \l QMetaType::QByteArray, \l QMetaType::Int, \l QMetaType::LongLong, \l QMetaType::QString, \l QMetaType::UInt, or \l QMetaType::ULongLong; otherwise returns 0.0. | |
| float | toFloat (bool *ok=nullptr) const |
| Returns the variant as a float if the variant has userType() \l QMetaType::Double, \l QMetaType::Float, \l QMetaType::Bool, \l QMetaType::QByteArray, \l QMetaType::Int, \l QMetaType::LongLong, \l QMetaType::QString, \l QMetaType::UInt, or \l QMetaType::ULongLong; otherwise returns 0.0. | |
| qreal | toReal (bool *ok=nullptr) const |
| Returns the variant as a qreal if the variant has userType() \l QMetaType::Double, \l QMetaType::Float, \l QMetaType::Bool, \l QMetaType::QByteArray, \l QMetaType::Int, \l QMetaType::LongLong, \l QMetaType::QString, \l QMetaType::UInt, or \l QMetaType::ULongLong; otherwise returns 0.0. | |
| QByteArray | toByteArray () const |
| Returns the variant as a QByteArray if the variant has userType() \l QMetaType::QByteArray or \l QMetaType::QString (converted using QString::fromUtf8()); otherwise returns an empty byte array. | |
| QBitArray | toBitArray () const |
| Returns the variant as a QBitArray if the variant has userType() \l QMetaType::QBitArray; otherwise returns an empty bit array. | |
| QString | toString () const |
| Returns the variant as a QString if the variant has a userType() including, but not limited to: | |
| QStringList | toStringList () const |
| Returns the variant as a QStringList if the variant has userType() \l QMetaType::QStringList, \l QMetaType::QString, or \l QMetaType::QVariantList of a type that can be converted to QString; otherwise returns an empty list. | |
| QChar | toChar () const |
| Returns the variant as a QChar if the variant has userType() \l QMetaType::QChar, \l QMetaType::Int, or \l QMetaType::UInt; otherwise returns an invalid QChar. | |
| QDate | toDate () const |
| Returns the variant as a QDate if the variant has userType() \l QMetaType::QDate, \l QMetaType::QDateTime, or \l QMetaType::QString; otherwise returns an invalid date. | |
| QTime | toTime () const |
| Returns the variant as a QTime if the variant has userType() \l QMetaType::QTime, \l QMetaType::QDateTime, or \l QMetaType::QString; otherwise returns an invalid time. | |
| QDateTime | toDateTime () const |
| Returns the variant as a QDateTime if the variant has userType() \l QMetaType::QDateTime, \l QMetaType::QDate, or \l QMetaType::QString; otherwise returns an invalid date/time. | |
| QList< QVariant > | toList () const |
| Returns the variant as a QVariantList if the variant has userType() \l QMetaType::QVariantList. | |
| QMap< QString, QVariant > | toMap () const |
| Returns the variant as a QVariantMap if the variant has metaType() \l QMetaType::QVariantMap. | |
| QHash< QString, QVariant > | toHash () const |
| Returns the variant as a QHash<QString, QVariant> if the variant has metaType() \l QMetaType::QVariantHash. | |
| QPoint | toPoint () const |
| Returns the variant as a QPoint if the variant has userType() \l QMetaType::QPoint or \l QMetaType::QPointF; otherwise returns a null QPoint. | |
| QPointF | toPointF () const |
| Returns the variant as a QPointF if the variant has userType() \l QMetaType::QPoint or \l QMetaType::QPointF; otherwise returns a null QPointF. | |
| QRect | toRect () const |
| Returns the variant as a QRect if the variant has userType() \l QMetaType::QRect; otherwise returns an invalid QRect. | |
| QSize | toSize () const |
| Returns the variant as a QSize if the variant has userType() \l QMetaType::QSize; otherwise returns an invalid QSize. | |
| QSizeF | toSizeF () const |
| Returns the variant as a QSizeF if the variant has userType() \l QMetaType::QSizeF; otherwise returns an invalid QSizeF. | |
| QLine | toLine () const |
| Returns the variant as a QLine if the variant has userType() \l QMetaType::QLine; otherwise returns an invalid QLine. | |
| QLineF | toLineF () const |
| Returns the variant as a QLineF if the variant has userType() \l QMetaType::QLineF; otherwise returns an invalid QLineF. | |
| QRectF | toRectF () const |
| Returns the variant as a QRectF if the variant has userType() \l QMetaType::QRect or \l QMetaType::QRectF; otherwise returns an invalid QRectF. | |
| QLocale | toLocale () const |
| Returns the variant as a QLocale if the variant has userType() \l QMetaType::QLocale; otherwise returns an invalid QLocale. | |
| QUuid | toUuid () const |
| QUrl | toUrl () const |
| Returns the variant as a QUrl if the variant has userType() \l QMetaType::QUrl; otherwise returns an invalid QUrl. | |
| QJsonValue | toJsonValue () const |
| QJsonObject | toJsonObject () const |
| QJsonArray | toJsonArray () const |
| QJsonDocument | toJsonDocument () const |
| void | load (QDataStream &ds) |
| Internal function for loading a variant from stream s. | |
| void | save (QDataStream &ds) const |
| Internal function for saving a variant to the stream s. | |
| void * | data () |
| Returns a pointer to the contained object as a generic void* that can be written to. | |
| const void * | constData () const |
| const void * | data () const |
| Returns a pointer to the contained object as a generic void* that cannot be written to. | |
| template<typename T, typename... Args, if_constructible< T, Args... > = true> | |
| T & | emplace (Args &&... args) |
| template<typename T, typename U, typename... Args, if_constructible< T, std::initializer_list< U > &, Args... > = true> | |
| T & | emplace (std::initializer_list< U > list, Args &&... args) |
| template<typename T, typename = std::enable_if_t<!std::is_same_v<std::decay_t<T>, QVariant>>> | |
| void | setValue (T &&avalue) |
| void | setValue (const QVariant &avalue) |
| Copies value over this QVariant. | |
| void | setValue (QVariant &&avalue) |
| Moves value over this QVariant. | |
| template<typename T> | |
| T | value () const & |
| template<typename T> | |
| T | view () |
Returns a mutable view of template type {T} on the stored value. | |
| template<typename T> | |
| T | value () && |
| template<typename T> | |
| bool | canConvert () const |
Returns true if the variant can be converted to the template type {T}, otherwise false. | |
| template<typename T> | |
| bool | canView () const |
Returns true if a mutable view of the template type {T} can be created on this variant, otherwise false. | |
| DataPtr & | data_ptr () |
| const DataPtr & | data_ptr () const |
Static Public Member Functions | |
| template<typename T, if_rvalue< T > = true> | |
| static auto | fromValue (T &&value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::conjunction_v< std::is_copy_constructible< T >, std::is_destructible< T > >, QVariant > |
| template<typename T> | |
| static auto | fromValue (const T &value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::is_copy_constructible_v< T > &&std::is_destructible_v< T >, QVariant > |
| Returns a QVariant containing a copy of value. | |
| template<typename... Types> | |
| static QVariant | fromStdVariant (const std::variant< Types... > &value) |
| template<typename... Types> | |
| static QVariant | fromStdVariant (std::variant< Types... > &&value) |
| static QVariant | fromMetaType (QMetaType type, const void *copy=nullptr) |
| static QPartialOrdering | compare (const QVariant &lhs, const QVariant &rhs) |
| Compares the objects at lhs and rhs for ordering. | |
Protected Member Functions | |
| bool | equals (const QVariant &other) const |
Protected Attributes | |
| Private | d |
Friends | |
| bool | comparesEqual (const QVariant &a, const QVariant &b) |
| template<typename T> | |
| auto | operator<< (const QDebug &debug, const T &variant) -> std::enable_if_t< std::is_same_v< T, QVariant >, QDebug > |
| template<typename T> | |
| T * | get_if (QVariant *v) noexcept |
If v contains an object of type T, returns a pointer to the contained object, otherwise returns \nullptr. | |
| template<typename T> | |
| const T * | get_if (const QVariant *v) noexcept |
| template<typename T> | |
| T | qvariant_cast (const QVariant &v) |
Returns the given value converted to the template type {T}. | |
| template<typename T> | |
| T | qvariant_cast (QVariant &&v) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
Related Symbols | |
(Note that these are not member symbols.) | |
| QDataStream & | operator>> (QDataStream &s, QVariant &p) |
| QDataStream & | operator<< (QDataStream &s, const QVariant &p) |
| Writes a variant p to the stream s. | |
| QDataStream & | operator>> (QDataStream &s, QVariant::Type &p) |
| QDataStream & | operator<< (QDataStream &s, const QVariant::Type p) |
| template< typename T > T | qVariantValue (const QVariant &value) |
| bool | qVariantCanConvert (const QVariant &value) |
| QVariantList | |
| Synonym for QList<QVariant>. | |
| QVariantMap | |
| Synonym for QMap<QString, QVariant>. | |
| QVariantHash | |
\inmodule QtCore
The QVariant class acts like a union for the most common Qt data types.
\compares equality
A QVariant object holds a single value of a single typeId() at a time. (Some types 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:
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. A variant is null if the variant contains no initialized value, or contains a null pointer.
QVariant can be extended to support other types than those mentioned in the \l QMetaType::Type enum. See \l{Creating Custom Qt Types}{Creating Custom Qt Types} for details.
Definition at line 67 of file qvariant.h.
| typedef Private QVariant::DataPtr |
Definition at line 975 of file qvariant.h.
|
inlinenoexcept |
Constructs an invalid variant.
Definition at line 276 of file qvariant.h.
| QVariant::~QVariant | ( | ) |
Destroys the QVariant and the contained object.
Definition at line 524 of file qvariant.cpp.
Constructs a variant of type type, and initializes it with a copy of {*copy} if copy is not \nullptr (in which case, copy must point to an object of type type).
Note that you have to pass the address of the object 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, and QMetaType::QObjectStar.
If type does not support copy construction and copy is not \nullptr, the variant will be invalid. Similarly, if copy is \nullptr and type does not support default construction, the variant will be invalid.
Definition at line 1027 of file qvariant.cpp.
| QVariant::QVariant | ( | const QVariant & | other | ) |
Constructs a copy of the variant, p, passed as the argument to this constructor.
Definition at line 537 of file qvariant.cpp.
|
inlineexplicitnoexcept |
Definition at line 453 of file qvariant.h.
|
inlineexplicitnoexcept |
Definition at line 463 of file qvariant.h.
|
noexcept |
Constructs a new variant with an integer value, val.
|
noexcept |
Constructs a new variant with an unsigned integer value, val.
|
noexcept |
Constructs a new variant with a long long integer value, val.
|
noexcept |
Constructs a new variant with an unsigned long long integer value, val.
|
noexcept |
Constructs a new variant with a boolean value, val.
|
noexcept |
Constructs a new variant with a floating point value, val.
|
noexcept |
Constructs a new variant with a floating point value, val.
|
noexcept |
Constructs a new variant with a char value, c.
|
noexcept |
Constructs a new variant with a date value, val.
|
noexcept |
Constructs a new variant with a time value, val.
|
noexcept |
Constructs a new variant with a bitarray value, val.
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
noexcept |
Constructs a new variant with a bytearray value, val.
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
noexcept |
Constructs a new variant with a date/time value, val.
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Constructs a new variant with a hash of \l {QVariant}s, val.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
noexcept |
Constructs a new variant with a json array value, val.
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
noexcept |
Constructs a new variant with a json object value, val.
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Constructs a new variant with a list value, val.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
noexcept |
Constructs a new variant with a locale value, l.
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Constructs a new variant with a map of \l {QVariant}s, val.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
noexcept |
Constructs a new variant with the regular expression value re.
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
noexcept |
Constructs a new variant with a string value, val.
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
noexcept |
Constructs a new variant with a string list value, val.
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
noexcept |
Constructs a new variant with a url value of val.
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
noexcept |
Constructs a new variant with a json value, val.
Definition at line 1090 of file qvariant.cpp.
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 1093 of file qvariant.cpp.
|
noexcept |
Constructs a new variant with a QModelIndex value, val.
|
noexcept |
Constructs a new variant with an uuid value, val.
|
noexcept |
Constructs a new variant with a size value of val.
|
noexcept |
Constructs a new variant with a size value of val.
|
noexcept |
Constructs a new variant with a point value of val.
|
noexcept |
Constructs a new variant with a point value of val.
|
noexcept |
Constructs a new variant with a line value of val.
|
noexcept |
Constructs a new variant with a line value of val.
|
noexcept |
Constructs a new variant with a rect value of val.
|
noexcept |
Constructs a new variant with a rect value of val.
| QVariant::QVariant | ( | const QEasingCurve & | val | ) |
Constructs a new variant with an easing curve value, val.
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
| QVariant::QVariant | ( | const QJsonDocument & | val | ) |
Constructs a new variant with a json document value, val.
Definition at line 1097 of file qvariant.cpp.
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 1098 of file qvariant.cpp.
| QVariant::QVariant | ( | const QPersistentModelIndex & | val | ) |
Constructs a new variant with a QPersistentModelIndex value, val.
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inline |
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.
Definition at line 538 of file qvariant.h.
| QVariant::QVariant | ( | QLatin1StringView | string | ) |
Constructs a new variant with a QString value from the Latin-1 string viewed by val.
Definition at line 1069 of file qvariant.cpp.
|
delete |
|
inlinenoexcept |
Move-constructs a QVariant instance, making it point at the same object that other was pointing to.
Definition at line 566 of file qvariant.h.
|
inline |
Returns true if the variant can be converted to the template type {T}, otherwise false.
Example:
A QVariant containing a pointer to a type derived from QObject will also return true for this function if a qobject_cast to the template type {T} would succeed. Note that this only works for QObject subclasses which use the Q_OBJECT macro.
Definition at line 835 of file qvariant.h.
|
inline |
Returns true if the variant's type can be cast to the requested type, type. Such casting is done automatically when calling the toInt(), toBool(), ... methods.
Note this function operates only on the variant's type, not the contents. It indicates whether there is a conversion path from this variant to type, not that the conversion will succeed when attempted.
Definition at line 589 of file qvariant.h.
|
inline |
Returns true if a mutable view of the template type {T} can be created on this variant, otherwise false.
Definition at line 839 of file qvariant.h.
|
inline |
Definition at line 593 of file qvariant.h.
| void QVariant::clear | ( | ) |
Convert this variant to type QMetaType::UnknownType and free up any resources used.
Definition at line 1224 of file qvariant.cpp.
|
static |
Compares the objects at lhs and rhs for ordering.
Returns QPartialOrdering::Unordered if comparison is not supported or the values are unordered. Otherwise, returns QPartialOrdering::Less, QPartialOrdering::Equivalent or QPartialOrdering::Greater if lhs is less than, equivalent to or greater than rhs, respectively.
If the variants contain data with a different metatype, the values are considered unordered unless they are both of numeric or pointer types, where regular numeric or pointer comparison rules will be used.
If both variants contain data of the same metatype, the method will use the QMetaType::compare method to determine the ordering of the two variants, which can also indicate that it can't establish an ordering between the two values.
Definition at line 2521 of file qvariant.cpp.
|
inline |
Definition at line 689 of file qvariant.h.
| bool QVariant::convert | ( | QMetaType | targetType | ) |
Casts the variant to the requested type, targetType.
convert(QMetaType(targetTypeId)) instead.Casts the variant to the requested type, targetTypeId. If the cast cannot be done, the variant is still changed to the requested type, but is left in a cleared null state similar to that constructed by QVariant(Type).
Returns true if the current type of the variant was successfully cast; otherwise returns false.
A QVariant containing a pointer to a type derived from QObject will also convert and return true for this function if a qobject_cast to the type described by targetTypeId would succeed. Note that this only works for QObject subclasses which use the Q_OBJECT macro.
false.If the cast cannot be done, the variant is still changed to the requested type, but is left in a cleared null state similar to that constructed by QVariant(Type).
Returns true if the current type of the variant was successfully cast; otherwise returns false.
A QVariant containing a pointer to a type derived from QObject will also convert and return true for this function if a qobject_cast to the type described by targetType would succeed. Note that this only works for QObject subclasses which use the Q_OBJECT macro.
false.convert(QMetaType(targetTypeId)) instead.Casts the variant to the requested type, targetTypeId. If the cast cannot be done, the variant is still changed to the requested type, but is left in a cleared null state similar to that constructed by QVariant(Type).
Returns true if the current type of the variant was successfully cast; otherwise returns false.
A QVariant containing a pointer to a type derived from QObject will also convert and return true for this function if a qobject_cast to the type described by targetTypeId would succeed. Note that this only works for QObject subclasses which use the Q_OBJECT macro.
false.Definition at line 2217 of file qvariant.cpp.
| void * QVariant::data | ( | ) |
|
inline |
Returns a pointer to the contained object as a generic void* that cannot be written to.
Definition at line 691 of file qvariant.h.
|
inline |
Definition at line 976 of file qvariant.h.
|
inline |
Definition at line 977 of file qvariant.h.
| void QVariant::detach | ( | ) |
Definition at line 1191 of file qvariant.cpp.
|
inline |
{*this} with an object of type {T}, constructed from {args}{...}. If {*this} was non-null, the previously held object is destroyed first. If possible, this method will reuse memory allocated by the QVariant. Returns a reference to the newly-created object. Definition at line 718 of file qvariant.h.
|
inline |
initializer_list. It behaves otherwise equivalent to the non-initializer list overload. Definition at line 725 of file qvariant.h.
|
protected |
Definition at line 2478 of file qvariant.cpp.
Creates a variant of type type, and initializes it with a copy of {*copy} if copy is not \nullptr (in which case, copy must point to an object of type type).
Note that you have to pass the address of the object 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, and QMetaType::QObjectStar.
If type does not support copy construction and copy is not \nullptr, the variant will be invalid. Similarly, if copy is \nullptr and type does not support default construction, the variant will be invalid.
Returns the QVariant created as described above.
Definition at line 2820 of file qvariant.cpp.
|
inlinestatic |
Returns a QVariant with the type and value of the active variant of value. If the active type is std::monostate a default QVariant is returned.
Definition at line 821 of file qvariant.h.
|
inlinestatic |
Definition at line 827 of file qvariant.h.
|
inlinestaticnoexcept |
Returns a QVariant containing a copy of value.
Behaves exactly like setValue() otherwise.
Example:
Definition at line 804 of file qvariant.h.
|
inlinestaticnoexcept |
Definition at line 775 of file qvariant.h.
|
inline |
Definition at line 1023 of file qvariant.h.
| bool QVariant::isNull | ( | ) | const |
Returns true if this is a null variant, false otherwise.
A variant is considered null if it contains no initialized value or a null pointer.
Definition at line 2609 of file qvariant.cpp.
|
inline |
Returns true if the storage type of this variant is not QMetaType::UnknownType; otherwise returns false.
Definition at line 980 of file qvariant.h.
| void QVariant::load | ( | QDataStream & | s | ) |
Internal function for loading a variant from stream s.
Use the stream operators instead.
Definition at line 1318 of file qvariant.cpp.
| QMetaType QVariant::metaType | ( | ) | const |
Returns the QMetaType of the value stored in the variant.
Move-assigns other to this QVariant instance.
Assigns the value of the variant variant to this variant.
Definition at line 1169 of file qvariant.cpp.
| void QVariant::save | ( | QDataStream & | s | ) | const |
Internal function for saving a variant to the stream s.
Use the stream operators instead.
Definition at line 1398 of file qvariant.cpp.
Copies value over this QVariant.
It is equivalent to simply assigning value to this QVariant.
Definition at line 744 of file qvariant.h.
Moves value over this QVariant.
It is equivalent to simply move assigning value to this QVariant.
Definition at line 749 of file qvariant.h.
|
inline |
Definition at line 731 of file qvariant.h.
Definition at line 570 of file qvariant.h.
| QBitArray QVariant::toBitArray | ( | ) | const |
Returns the variant as a QBitArray if the variant has userType() \l QMetaType::QBitArray; otherwise returns an empty bit array.
Definition at line 1954 of file qvariant.cpp.
| bool QVariant::toBool | ( | ) | const |
Returns the variant as a bool if the variant has userType() Bool.
Returns true if the variant has userType() \l QMetaType::Bool, \l QMetaType::QChar, \l QMetaType::Double, \l QMetaType::Int, \l QMetaType::LongLong, \l QMetaType::UInt, or \l QMetaType::ULongLong and the value is non-zero, or if the variant has type \l QMetaType::QString or \l QMetaType::QByteArray and its lower-case content is not one of the following: empty, "0" or "false"; otherwise returns false.
Definition at line 2066 of file qvariant.cpp.
| QByteArray QVariant::toByteArray | ( | ) | const |
Returns the variant as a QByteArray if the variant has userType() \l QMetaType::QByteArray or \l QMetaType::QString (converted using QString::fromUtf8()); otherwise returns an empty byte array.
Definition at line 1686 of file qvariant.cpp.
| QChar QVariant::toChar | ( | ) | const |
Returns the variant as a QChar if the variant has userType() \l QMetaType::QChar, \l QMetaType::Int, or \l QMetaType::UInt; otherwise returns an invalid QChar.
Definition at line 1943 of file qvariant.cpp.
| QDate QVariant::toDate | ( | ) | const |
Returns the variant as a QDate if the variant has userType() \l QMetaType::QDate, \l QMetaType::QDateTime, or \l QMetaType::QString; otherwise returns an invalid date.
If the metaType() is \l QMetaType::QString, an invalid date will be returned if the string cannot be parsed as a Qt::ISODate format date.
Definition at line 1622 of file qvariant.cpp.
| QDateTime QVariant::toDateTime | ( | ) | const |
Returns the variant as a QDateTime if the variant has userType() \l QMetaType::QDateTime, \l QMetaType::QDate, or \l QMetaType::QString; otherwise returns an invalid date/time.
If the metaType() is \l QMetaType::QString, an invalid date/time will be returned if the string cannot be parsed as a Qt::ISODate format date/time.
Definition at line 1656 of file qvariant.cpp.
| double QVariant::toDouble | ( | bool * | ok = nullptr | ) | const |
Returns the variant as a double if the variant has userType() \l QMetaType::Double, \l QMetaType::Float, \l QMetaType::Bool, \l QMetaType::QByteArray, \l QMetaType::Int, \l QMetaType::LongLong, \l QMetaType::QString, \l QMetaType::UInt, or \l QMetaType::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.
Definition at line 2089 of file qvariant.cpp.
| float QVariant::toFloat | ( | bool * | ok = nullptr | ) | const |
Returns the variant as a float if the variant has userType() \l QMetaType::Double, \l QMetaType::Float, \l QMetaType::Bool, \l QMetaType::QByteArray, \l QMetaType::Int, \l QMetaType::LongLong, \l QMetaType::QString, \l QMetaType::UInt, or \l QMetaType::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.
Definition at line 2108 of file qvariant.cpp.
| QVariantHash QVariant::toHash | ( | ) | const |
Returns the variant as a QHash<QString, QVariant> if the variant has metaType() \l QMetaType::QVariantHash.
If it doesn't, QVariant will attempt to convert the type to a hash and then return it. This will succeed for any type that has registered a converter to QVariantHash or which was declared as a associative container using \l{Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE}. If none of those conditions are true, this function will return an empty hash.
Definition at line 1605 of file qvariant.cpp.
| int QVariant::toInt | ( | bool * | ok = nullptr | ) | const |
Returns the variant as an int if the variant has userType() \l QMetaType::Int, \l QMetaType::Bool, \l QMetaType::QByteArray, \l QMetaType::QChar, \l QMetaType::Double, \l QMetaType::LongLong, \l QMetaType::QString, \l QMetaType::UInt, or \l QMetaType::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 \l QMetaType::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().
Definition at line 1993 of file qvariant.cpp.
| QJsonArray QVariant::toJsonArray | ( | ) | const |
Returns the variant as a QJsonArray if the variant has userType() \l QJsonArray; otherwise returns a default constructed QJsonArray.
Definition at line 1916 of file qvariant.cpp.
| QJsonDocument QVariant::toJsonDocument | ( | ) | const |
Returns the variant as a QJsonDocument if the variant has userType() \l QJsonDocument; otherwise returns a default constructed QJsonDocument.
Definition at line 1929 of file qvariant.cpp.
| QJsonObject QVariant::toJsonObject | ( | ) | const |
Returns the variant as a QJsonObject if the variant has userType() \l QJsonObject; otherwise returns a default constructed QJsonObject.
Definition at line 1903 of file qvariant.cpp.
| QJsonValue QVariant::toJsonValue | ( | ) | const |
Returns the variant as a QJsonValue if the variant has userType() \l QJsonValue; otherwise returns a default constructed QJsonValue.
Definition at line 1890 of file qvariant.cpp.
| QLine QVariant::toLine | ( | ) | const |
Returns the variant as a QLine if the variant has userType() \l QMetaType::QLine; otherwise returns an invalid QLine.
Definition at line 1779 of file qvariant.cpp.
| QLineF QVariant::toLineF | ( | ) | const |
Returns the variant as a QLineF if the variant has userType() \l QMetaType::QLineF; otherwise returns an invalid QLineF.
Definition at line 1766 of file qvariant.cpp.
| QVariantList QVariant::toList | ( | ) | const |
Returns the variant as a QVariantList if the variant has userType() \l QMetaType::QVariantList.
If it doesn't, QVariant will attempt to convert the type to a list and then return it. This will succeed for any type that has registered a converter to QVariantList or which was declared as a sequential container using \l{Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE}. If none of those conditions are true, this function will return an empty list.
Definition at line 2143 of file qvariant.cpp.
| QLocale QVariant::toLocale | ( | ) | const |
Returns the variant as a QLocale if the variant has userType() \l QMetaType::QLocale; otherwise returns an invalid QLocale.
Definition at line 1819 of file qvariant.cpp.
Returns the variant as a long long int if the variant has userType() \l QMetaType::LongLong, \l QMetaType::Bool, \l QMetaType::QByteArray, \l QMetaType::QChar, \l QMetaType::Double, \l QMetaType::Int, \l QMetaType::QString, \l QMetaType::UInt, or \l QMetaType::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.
Definition at line 2032 of file qvariant.cpp.
| QVariantMap QVariant::toMap | ( | ) | const |
Returns the variant as a QVariantMap if the variant has metaType() \l QMetaType::QVariantMap.
If it doesn't, QVariant will attempt to convert the type to a map and then return it. This will succeed for any type that has registered a converter to QVariantMap or which was declared as a associative container using \l{Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE}. If none of those conditions are true, this function will return an empty map.
Definition at line 1589 of file qvariant.cpp.
| QPoint QVariant::toPoint | ( | ) | const |
Returns the variant as a QPoint if the variant has userType() \l QMetaType::QPoint or \l QMetaType::QPointF; otherwise returns a null QPoint.
Definition at line 1700 of file qvariant.cpp.
| QPointF QVariant::toPointF | ( | ) | const |
Returns the variant as a QPointF if the variant has userType() \l QMetaType::QPoint or \l QMetaType::QPointF; otherwise returns a null QPointF.
Definition at line 1793 of file qvariant.cpp.
Returns the variant as a qreal if the variant has userType() \l QMetaType::Double, \l QMetaType::Float, \l QMetaType::Bool, \l QMetaType::QByteArray, \l QMetaType::Int, \l QMetaType::LongLong, \l QMetaType::QString, \l QMetaType::UInt, or \l QMetaType::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.
Definition at line 2127 of file qvariant.cpp.
| QRect QVariant::toRect | ( | ) | const |
Returns the variant as a QRect if the variant has userType() \l QMetaType::QRect; otherwise returns an invalid QRect.
Definition at line 1713 of file qvariant.cpp.
| QRectF QVariant::toRectF | ( | ) | const |
Returns the variant as a QRectF if the variant has userType() \l QMetaType::QRect or \l QMetaType::QRectF; otherwise returns an invalid QRectF.
Definition at line 1753 of file qvariant.cpp.
| QSize QVariant::toSize | ( | ) | const |
Returns the variant as a QSize if the variant has userType() \l QMetaType::QSize; otherwise returns an invalid QSize.
Definition at line 1726 of file qvariant.cpp.
| QSizeF QVariant::toSizeF | ( | ) | const |
Returns the variant as a QSizeF if the variant has userType() \l QMetaType::QSizeF; otherwise returns an invalid QSizeF.
Definition at line 1739 of file qvariant.cpp.
| QString QVariant::toString | ( | ) | const |
Returns the variant as a QString if the variant has a userType() including, but not limited to:
\l QMetaType::QString, \l QMetaType::Bool, \l QMetaType::QByteArray, \l QMetaType::QChar, \l QMetaType::QDate, \l QMetaType::QDateTime, \l QMetaType::Double, \l QMetaType::Int, \l QMetaType::LongLong, \l QMetaType::QStringList, \l QMetaType::QTime, \l QMetaType::UInt, or \l QMetaType::ULongLong.
Calling QVariant::toString() on an unsupported variant returns an empty string.
Definition at line 1573 of file qvariant.cpp.
| QStringList QVariant::toStringList | ( | ) | const |
Returns the variant as a QStringList if the variant has userType() \l QMetaType::QStringList, \l QMetaType::QString, or \l QMetaType::QVariantList of a type that can be converted to QString; otherwise returns an empty list.
Definition at line 1553 of file qvariant.cpp.
| QTime QVariant::toTime | ( | ) | const |
Returns the variant as a QTime if the variant has userType() \l QMetaType::QTime, \l QMetaType::QDateTime, or \l QMetaType::QString; otherwise returns an invalid time.
If the metaType() is \l QMetaType::QString, an invalid time will be returned if the string cannot be parsed as a Qt::ISODate format time.
Definition at line 1639 of file qvariant.cpp.
Returns the variant as an unsigned int if the variant has userType() \l QMetaType::UInt, \l QMetaType::Bool, \l QMetaType::QByteArray, \l QMetaType::QChar, \l QMetaType::Double, \l QMetaType::Int, \l QMetaType::LongLong, \l QMetaType::QString, or \l QMetaType::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 \l QMetaType::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().
Definition at line 2015 of file qvariant.cpp.
| qulonglong QVariant::toULongLong | ( | bool * | ok = nullptr | ) | const |
Returns the variant as an unsigned long long int if the variant has metaType() \l QMetaType::ULongLong, \l QMetaType::Bool, \l QMetaType::QByteArray, \l QMetaType::QChar, \l QMetaType::Double, \l QMetaType::Int, \l QMetaType::LongLong, \l QMetaType::QString, or \l QMetaType::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.
Definition at line 2049 of file qvariant.cpp.
| QUrl QVariant::toUrl | ( | ) | const |
Returns the variant as a QUrl if the variant has userType() \l QMetaType::QUrl; otherwise returns an invalid QUrl.
Definition at line 1806 of file qvariant.cpp.
| QUuid QVariant::toUuid | ( | ) | const |
Returns the variant as a QUuid if the variant has metaType() \l QMetaType::QUuid, \l QMetaType::QByteArray or \l QMetaType::QString; otherwise returns a default-constructed QUuid.
Definition at line 1877 of file qvariant.cpp.
|
inline |
Returns the storage type of the value stored in the variant.
This is the same as metaType().id().
Definition at line 573 of file qvariant.h.
| 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.
|
inline |
Definition at line 572 of file qvariant.h.
|
inline |
Definition at line 767 of file qvariant.h.
|
inline |
Definition at line 755 of file qvariant.h.
|
inline |
Returns a mutable view of template type {T} on the stored value.
Call canView() to find out whether such a view is supported. If no such view can be created, returns the stored value converted to the template type {T}. Call canConvert() to find out whether a type can be converted. If the value can neither be viewed nor converted, a \l{default-constructed value} will be returned.
Definition at line 759 of file qvariant.h.
Definition at line 856 of file qvariant.h.
|
friend |
Definition at line 904 of file qvariant.h.
|
friend |
If v contains an object of type T, returns a pointer to the contained object, otherwise returns \nullptr.
The overload taking a mutable v detaches v: When called on a \l{isNull()}{null} v with matching type T, v will not be null after the call.
These functions are provided for compatibility with {std::variant}.
Definition at line 897 of file qvariant.h.
|
friend |
Definition at line 862 of file qvariant.h.
|
Writes a variant p to the stream s.
Definition at line 1515 of file qvariant.cpp.
|
Writes a variant type p to the stream s.
Writes a variant type p to the stream s.
|
Reads a variant p from the stream s.
\quotation QVariant::load: unknown user type with name QList<int> \endquotation
Definition at line 1503 of file qvariant.cpp.
|
Reads a variant type p in enum representation from the stream s.
Reads a variant type p in enum representation from the stream s.
|
friend |
Returns the given value converted to the template type {T}.
This function is equivalent to QVariant::value().
Definition at line 1047 of file qvariant.h.
|
friend |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns the given value converted to the template type {T}.
Definition at line 1056 of file qvariant.h.
|
Returns true if the given value can be converted to the template type specified; otherwise returns false.
This function is equivalent to QVariant::canConvert(value).
Returns true if the given value can be converted to the template type specified; otherwise returns false.
This function is equivalent to QVariant::canConvert(value).
Synonym for QList<QVariant>.
Definition at line 16 of file qjsonarray.h.
Synonym for QMap<QString, QVariant>.
Definition at line 53 of file qcontainerfwd.h.
|
Returns the given value converted to the template type {T}.
This function is equivalent to \l{QVariant::value()}{QVariant::value}<T>(value).
Returns the given value converted to the template type {T}.
This function is equivalent to \l{QVariant::value()}{QVariant::value}<T>(value).
|
protected |
Definition at line 930 of file qvariant.h.