QtBase  v6.3.1
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Friends | Related Functions | List of all members
QVariant Class Reference

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

#include <qvariant.h>

Collaboration diagram for QVariant:
Collaboration graph
[legend]

Classes

struct  Private
 
struct  PrivateShared
 

Public Types

typedef Private DataPtr
 

Public Member Functions

 QVariant () noexcept
 
 ~QVariant ()
 
 QVariant (QMetaType type, const void *copy=nullptr)
 
 QVariant (const QVariant &other)
 
 QVariant (int i)
 
 QVariant (uint ui)
 
 QVariant (qlonglong ll)
 
 QVariant (qulonglong ull)
 
 QVariant (bool b)
 
 QVariant (double d)
 
 QVariant (float f)
 
QT_ASCII_CAST_WARN QVariant (const char *str)
 
 QVariant (const QByteArray &bytearray)
 
 QVariant (const QBitArray &bitarray)
 
 QVariant (const QString &string)
 
 QVariant (QLatin1String string)
 
 QVariant (const QStringList &stringlist)
 
 QVariant (QChar qchar)
 
 QVariant (QDate date)
 
 QVariant (QTime time)
 
 QVariant (const QDateTime &datetime)
 
 QVariant (const QList< QVariant > &list)
 
 QVariant (const QMap< QString, QVariant > &map)
 
 QVariant (const QHash< QString, QVariant > &hash)
 
 QVariant (const QSize &size)
 
 QVariant (const QSizeF &size)
 
 QVariant (const QPoint &pt)
 
 QVariant (const QPointF &pt)
 
 QVariant (const QLine &line)
 
 QVariant (const QLineF &line)
 
 QVariant (const QRect &rect)
 
 QVariant (const QRectF &rect)
 
 QVariant (const QLocale &locale)
 
 QVariant (const QUuid &uuid)
 
 QVariant (const QUrl &url)
 
 QVariant (const QJsonValue &jsonValue)
 
 QVariant (const QJsonObject &jsonObject)
 
 QVariant (const QJsonArray &jsonArray)
 
 QVariant (const QJsonDocument &jsonDocument)
 
QVariantoperator= (const QVariant &other)
 
 QVariant (QVariant &&other) noexcept
 
void swap (QVariant &other) noexcept
 
int userType () const
 
int typeId () const
 
const char * typeName () const
 
QMetaType metaType () const
 
bool canConvert (QMetaType targetType) const
 
bool convert (QMetaType type)
 
bool canView (QMetaType targetType) const
 
bool isValid () const
 
bool isNull () const
 
void clear ()
 
void detach ()
 
bool isDetached () const
 
int toInt (bool *ok=nullptr) const
 
uint toUInt (bool *ok=nullptr) const
 
qlonglong toLongLong (bool *ok=nullptr) const
 
qulonglong toULongLong (bool *ok=nullptr) const
 
bool toBool () const
 
double toDouble (bool *ok=nullptr) const
 
float toFloat (bool *ok=nullptr) const
 
qreal toReal (bool *ok=nullptr) const
 
QByteArray toByteArray () const
 
QBitArray toBitArray () const
 
QString toString () const
 
QStringList toStringList () const
 
QChar toChar () const
 
QDate toDate () const
 
QTime toTime () const
 
QDateTime toDateTime () const
 
QList< QVarianttoList () const
 
QMap< QString, QVarianttoMap () const
 
QHash< QString, QVarianttoHash () const
 
QPoint toPoint () const
 
QPointF toPointF () const
 
QRect toRect () const
 
QSize toSize () const
 
QSizeF toSizeF () const
 
QLine toLine () const
 
QLineF toLineF () const
 
QRectF toRectF () const
 
QLocale toLocale () const
 
QUuid toUuid () const
 
QUrl toUrl () const
 
QJsonValue toJsonValue () const
 
QJsonObject toJsonObject () const
 
QJsonArray toJsonArray () const
 
QJsonDocument toJsonDocument () const
 
void load (QDataStream &ds)
 
void save (QDataStream &ds) const
 
voiddata ()
 
const voidconstData () const
 
const voiddata () const
 
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)
 
void setValue (QVariant &&avalue)
 
template<typename T >
T value () const
 
template<typename T >
T view ()
 
template<typename T >
bool canConvert () const
 
template<typename T >
bool canView () const
 
DataPtrdata_ptr ()
 
const DataPtrdata_ptr () const
 
template<>
QVariant fromValue (const QVariant &value)
 
template<>
QVariant fromValue (const std::monostate &)
 

Static Public Member Functions

template<typename T >
static auto fromValue (const T &value) -> std::enable_if_t< std::is_copy_constructible_v< T >, QVariant >
 
template<typename... Types>
static QVariant fromStdVariant (const std::variant< Types... > &value)
 
static QPartialOrdering compare (const QVariant &lhs, const QVariant &rhs)
 

Protected Member Functions

void create (int type, const void *copy)
 
void create (QMetaType type, const void *copy)
 
bool equals (const QVariant &other) const
 
bool convert (int type, void *ptr) const
 
bool view (int type, void *ptr)
 

Protected Attributes

Private d
 

Friends

bool operator== (const QVariant &a, const QVariant &b)
 
bool operator!= (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 qvariant_cast (const QVariant &)
 

Related Functions

(Note that these are not member functions.)

template< typename T > T qVariantValue (const QVariant &value)
 
bool qVariantCanConvert (const QVariant &value)
 
 QVariantList
 
 QVariantMap
 
 QVariantHash
 

Detailed Description

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

\inmodule QtCore

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

A QVariant object holds a single value of a single 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 94 of file qvariant.h.

Member Typedef Documentation

◆ DataPtr

Definition at line 565 of file qvariant.h.

Constructor & Destructor Documentation

◆ QVariant() [1/39]

QVariant::QVariant ( )
inlinenoexcept

Constructs an invalid variant.

Definition at line 173 of file qvariant.h.

Here is the caller graph for this function:

◆ ~QVariant()

QVariant::~QVariant ( )

Destroys the QVariant and the contained object.

Note that subclasses that reimplement clear() should reimplement the destructor to call clear(). This destructor calls clear(), but because it is the destructor, QVariant::clear() is called rather than a subclass's clear().

Definition at line 512 of file qvariant.cpp.

◆ QVariant() [2/39]

QVariant::QVariant ( QMetaType  type,
const void copy = nullptr 
)
explicit

Constructs variant of type type, and initializes with copy if copy is not \nullptr.

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, and QMetaType::QObjectStar.

See also
QVariant::fromValue(), QMetaType::Type

Definition at line 819 of file qvariant.cpp.

◆ QVariant() [3/39]

QVariant::QVariant ( const QVariant p)

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

Definition at line 525 of file qvariant.cpp.

Here is the call graph for this function:

◆ QVariant() [4/39]

QVariant::QVariant ( int  val)

Constructs a new variant with an integer value, val.

Definition at line 824 of file qvariant.cpp.

Here is the call graph for this function:

◆ QVariant() [5/39]

QVariant::QVariant ( uint  val)

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

Definition at line 827 of file qvariant.cpp.

Here is the call graph for this function:

◆ QVariant() [6/39]

QVariant::QVariant ( qlonglong  val)

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

Definition at line 830 of file qvariant.cpp.

Here is the call graph for this function:

◆ QVariant() [7/39]

QVariant::QVariant ( qulonglong  val)

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

Definition at line 833 of file qvariant.cpp.

Here is the call graph for this function:

◆ QVariant() [8/39]

QVariant::QVariant ( bool  val)

Constructs a new variant with a boolean value, val.

Definition at line 836 of file qvariant.cpp.

Here is the call graph for this function:

◆ QVariant() [9/39]

QVariant::QVariant ( double  val)

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

Definition at line 839 of file qvariant.cpp.

Here is the call graph for this function:

◆ QVariant() [10/39]

QVariant::QVariant ( float  val)

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

Since
4.6

Definition at line 842 of file qvariant.cpp.

Here is the call graph for this function:

◆ QVariant() [11/39]

QVariant::QVariant ( const char *  val)
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 186 of file qvariant.h.

◆ QVariant() [12/39]

QVariant::QVariant ( const QByteArray val)

Constructs a new variant with a bytearray value, val.

Definition at line 846 of file qvariant.cpp.

◆ QVariant() [13/39]

QVariant::QVariant ( const QBitArray val)

Constructs a new variant with a bitarray value, val.

Definition at line 849 of file qvariant.cpp.

◆ QVariant() [14/39]

QVariant::QVariant ( const QString val)

Constructs a new variant with a string value, val.

Definition at line 852 of file qvariant.cpp.

◆ QVariant() [15/39]

QVariant::QVariant ( QLatin1String  val)

Constructs a new variant with a string value, val.

Definition at line 858 of file qvariant.cpp.

◆ QVariant() [16/39]

QVariant::QVariant ( const QStringList val)

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

Definition at line 861 of file qvariant.cpp.

◆ QVariant() [17/39]

QVariant::QVariant ( QChar  c)

Constructs a new variant with a char value, c.

Definition at line 855 of file qvariant.cpp.

◆ QVariant() [18/39]

QVariant::QVariant ( QDate  val)

Constructs a new variant with a date value, val.

Definition at line 865 of file qvariant.cpp.

◆ QVariant() [19/39]

QVariant::QVariant ( QTime  val)

Constructs a new variant with a time value, val.

Definition at line 868 of file qvariant.cpp.

◆ QVariant() [20/39]

QVariant::QVariant ( const QDateTime val)

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

Definition at line 871 of file qvariant.cpp.

◆ QVariant() [21/39]

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

Constructs a new variant with a list value, val.

Definition at line 879 of file qvariant.cpp.

◆ QVariant() [22/39]

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

Constructs a new variant with a map of \l {QVariant}s, val.

Definition at line 882 of file qvariant.cpp.

◆ QVariant() [23/39]

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

Constructs a new variant with a hash of \l {QVariant}s, val.

Definition at line 885 of file qvariant.cpp.

◆ QVariant() [24/39]

QVariant::QVariant ( const QSize val)

Constructs a new variant with a size value of val.

Definition at line 907 of file qvariant.cpp.

◆ QVariant() [25/39]

QVariant::QVariant ( const QSizeF val)

Constructs a new variant with a size value of val.

Definition at line 910 of file qvariant.cpp.

◆ QVariant() [26/39]

QVariant::QVariant ( const QPoint val)

Constructs a new variant with a point value of val.

Definition at line 889 of file qvariant.cpp.

◆ QVariant() [27/39]

QVariant::QVariant ( const QPointF val)

Constructs a new variant with a point value of val.

Definition at line 892 of file qvariant.cpp.

◆ QVariant() [28/39]

QVariant::QVariant ( const QLine val)

Constructs a new variant with a line value of val.

Definition at line 901 of file qvariant.cpp.

◆ QVariant() [29/39]

QVariant::QVariant ( const QLineF val)

Constructs a new variant with a line value of val.

Definition at line 898 of file qvariant.cpp.

◆ QVariant() [30/39]

QVariant::QVariant ( const QRect val)

Constructs a new variant with a rect value of val.

Definition at line 904 of file qvariant.cpp.

◆ QVariant() [31/39]

QVariant::QVariant ( const QRectF val)

Constructs a new variant with a rect value of val.

Definition at line 895 of file qvariant.cpp.

◆ QVariant() [32/39]

QVariant::QVariant ( const QLocale l)

Constructs a new variant with a locale value, l.

Definition at line 919 of file qvariant.cpp.

◆ QVariant() [33/39]

QVariant::QVariant ( const QUuid val)
Since
5.0

Constructs a new variant with an uuid value, val.

Definition at line 927 of file qvariant.cpp.

◆ QVariant() [34/39]

QVariant::QVariant ( const QUrl val)

Constructs a new variant with a url value of val.

Definition at line 915 of file qvariant.cpp.

◆ QVariant() [35/39]

QVariant::QVariant ( const QJsonValue val)
Since
5.0

Constructs a new variant with a json value, val.

Definition at line 931 of file qvariant.cpp.

◆ QVariant() [36/39]

QVariant::QVariant ( const QJsonObject val)
Since
5.0

Constructs a new variant with a json object value, val.

Definition at line 934 of file qvariant.cpp.

◆ QVariant() [37/39]

QVariant::QVariant ( const QJsonArray val)
Since
5.0

Constructs a new variant with a json array value, val.

Definition at line 937 of file qvariant.cpp.

◆ QVariant() [38/39]

QVariant::QVariant ( const QJsonDocument val)
Since
5.0

Constructs a new variant with a json document value, val.

Definition at line 940 of file qvariant.cpp.

◆ QVariant() [39/39]

QVariant::QVariant ( QVariant &&  other)
inlinenoexcept

Move-constructs a QVariant instance, making it point at the same object that other was pointing to.

Since
5.2

Definition at line 234 of file qvariant.h.

Here is the call graph for this function:

Member Function Documentation

◆ canConvert() [1/2]

template<typename T >
bool QVariant::canConvert ( ) const
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.

See also
convert()

Definition at line 409 of file qvariant.h.

◆ canConvert() [2/2]

bool QVariant::canConvert ( QMetaType  type) const
inline
Since
6.0

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.

See also
QMetaType::canConvert()

Definition at line 246 of file qvariant.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ canView() [1/2]

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

Returns true if a mutable view of the template type {T} can be created on this variant, otherwise false.

See also
value()

Definition at line 413 of file qvariant.h.

◆ canView() [2/2]

bool QVariant::canView ( QMetaType  targetType) const
inline

Definition at line 250 of file qvariant.h.

Here is the call graph for this function:

◆ clear()

void QVariant::clear ( )

Convert this variant to type QMetaType::UnknownType and free up any resources used.

Definition at line 1082 of file qvariant.cpp.

Here is the caller graph for this function:

◆ compare()

QPartialOrdering QVariant::compare ( const QVariant lhs,
const QVariant rhs 
)
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.

Note
: If a numeric comparison is done and at least one value is NaN, QPartialOrdering::Unordered is returned.

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.

Since
6.0
See also
QMetaType::compare(), QMetaType::isOrdered()

Definition at line 2429 of file qvariant.cpp.

Here is the call graph for this function:

◆ constData()

const void * QVariant::constData ( ) const
inline

Definition at line 350 of file qvariant.h.

Here is the caller graph for this function:

◆ convert() [1/2]

bool QVariant::convert ( int  type,
void ptr 
) const
protected

Definition at line 2075 of file qvariant.cpp.

◆ convert() [2/2]

bool QVariant::convert ( QMetaType  targetType)

Casts the variant to the requested type, targetType. 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.

Note
converting QVariants that are null due to not being initialized or having failed a previous conversion will always fail, changing the type, remaining null, and returning false.
Since
6.0
See also
canConvert(), clear()

Definition at line 2049 of file qvariant.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ create() [1/2]

QVariant::create ( int  type,
const void copy 
)
protected

Definition at line 484 of file qvariant.cpp.

Here is the caller graph for this function:

◆ create() [2/2]

void QVariant::create ( QMetaType  type,
const void copy 
)
protected

Definition at line 495 of file qvariant.cpp.

◆ data() [1/2]

void * QVariant::data ( )

Returns a pointer to the contained object as a generic void* that can be written to.

This function detaches the QVariant. When called on a \l{isNull}{null-QVariant}, the QVariant will not be null after the call.

See also
QMetaType

Definition at line 2464 of file qvariant.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ data() [2/2]

const void * QVariant::data ( ) const
inline

Returns a pointer to the contained object as a generic void* that cannot be written to.

See also
QMetaType

Definition at line 352 of file qvariant.h.

Here is the caller graph for this function:

◆ data_ptr() [1/2]

DataPtr & QVariant::data_ptr ( )
inline

Definition at line 566 of file qvariant.h.

◆ data_ptr() [2/2]

const DataPtr & QVariant::data_ptr ( ) const
inline

Definition at line 567 of file qvariant.h.

◆ detach()

void QVariant::detach ( )

Definition at line 1049 of file qvariant.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ equals()

bool QVariant::equals ( const QVariant other) const
protected

Definition at line 2372 of file qvariant.cpp.

Here is the call graph for this function:

◆ fromStdVariant()

template<typename... Types>
template< typename... Types > QVariant QVariant::fromStdVariant ( const std::variant< Types... > &  value)
inlinestatic
Since
5.11

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.

Note
With this method you do not need to register the variant as a Qt metatype, since the std::variant is resolved before being stored. The component types should be registered however.
See also
fromValue()

Definition at line 401 of file qvariant.h.

◆ fromValue() [1/3]

template<>
QVariant QVariant::fromValue ( const QVariant value)
inline

Definition at line 571 of file qvariant.h.

Here is the call graph for this function:

◆ fromValue() [2/3]

template<>
QVariant QVariant::fromValue ( const std::monostate &  )
inline

Definition at line 577 of file qvariant.h.

Here is the call graph for this function:

◆ fromValue() [3/3]

template<typename T >
template< typename T > static QVariant QVariant::fromValue ( const T value) -> std::enable_if_t<std::is_copy_constructible_v<T>, QVariant>
inlinestatic

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

Example:

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

Definition at line 391 of file qvariant.h.

Here is the caller graph for this function:

◆ isDetached()

bool QVariant::isDetached ( ) const
inline

Definition at line 613 of file qvariant.h.

◆ isNull()

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.

Note
This behavior has been changed from Qt 5, where isNull() would also return true if the variant contained an object of a builtin type with an isNull() method that returned true for that object.
See also
convert()

Definition at line 2483 of file qvariant.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isValid()

bool QVariant::isValid ( ) const
inline

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

Definition at line 582 of file qvariant.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ load()

void QVariant::load ( QDataStream s)

Internal function for loading a variant from stream s. Use the stream operators instead.

Definition at line 1176 of file qvariant.cpp.

Here is the call graph for this function:

◆ metaType()

QMetaType QVariant::metaType ( ) const
Since
6.0

Returns the QMetaType of the value stored in the variant.

Definition at line 1003 of file qvariant.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=()

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

Move-assigns other to this QVariant instance.

Since
5.2

Assigns the value of the variant variant to this variant.

Definition at line 1011 of file qvariant.cpp.

Here is the call graph for this function:

◆ save()

void QVariant::save ( QDataStream s) const

Internal function for saving a variant to the stream s. Use the stream operators instead.

Definition at line 1256 of file qvariant.cpp.

Here is the call graph for this function:

◆ setValue() [1/3]

template< typename T > void QVariant::setValue ( const QVariant value)
inline

Copies value over this QVariant. It is equivalent to simply assigning value to this QVariant.

Definition at line 367 of file qvariant.h.

◆ setValue() [2/3]

template< typename T > void QVariant::setValue ( QVariant &&  value)
inline

Moves value over this QVariant. It is equivalent to simply move assigning value to this QVariant.

Definition at line 372 of file qvariant.h.

◆ setValue() [3/3]

template<typename T , typename = std::enable_if_t<!std::is_same_v<std::decay_t<T>, QVariant>>>
template< typename T > void QVariant::setValue ( T &&  value)
inline

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:

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

Definition at line 355 of file qvariant.h.

Here is the caller graph for this function:

◆ swap()

void QVariant::swap ( QVariant other)
inlinenoexcept
Since
4.8

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

Definition at line 238 of file qvariant.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toBitArray()

QBitArray QVariant::toBitArray ( ) const

Returns the variant as a QBitArray if the variant has userType() \l QMetaType::QBitArray; otherwise returns an empty bit array.

See also
canConvert(), convert()

Definition at line 1794 of file qvariant.cpp.

◆ toBool()

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.

See also
canConvert(), convert()

Definition at line 1906 of file qvariant.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toByteArray()

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.

See also
canConvert(), convert()

Definition at line 1519 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toChar()

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.

See also
canConvert(), convert()

Definition at line 1783 of file qvariant.cpp.

◆ toDate()

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 type() is \l QMetaType::QString, an invalid date will be returned if the string cannot be parsed as a Qt::ISODate format date.

See also
canConvert(), convert()

Definition at line 1455 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toDateTime()

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 type() is \l QMetaType::QString, an invalid date/time will be returned if the string cannot be parsed as a Qt::ISODate format date/time.

See also
canConvert(), convert()

Definition at line 1489 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toDouble()

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.

See also
canConvert(), convert()

Definition at line 1929 of file qvariant.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toFloat()

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.

Since
4.6

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(), convert()

Definition at line 1948 of file qvariant.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toHash()

QVariantHash QVariant::toHash ( ) const

Returns the variant as a QHash<QString, QVariant> if the variant has type() \l QMetaType::QVariantHash; otherwise returns an empty map.

See also
canConvert(), convert()

Definition at line 1438 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toInt()

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

See also
canConvert(), convert()

Definition at line 1833 of file qvariant.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toJsonArray()

QJsonArray QVariant::toJsonArray ( ) const
Since
5.0

Returns the variant as a QJsonArray if the variant has userType() \l QJsonArray; otherwise returns a default constructed QJsonArray.

See also
canConvert(), convert()

Definition at line 1755 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toJsonDocument()

QJsonDocument QVariant::toJsonDocument ( ) const
Since
5.0

Returns the variant as a QJsonDocument if the variant has userType() \l QJsonDocument; otherwise returns a default constructed QJsonDocument.

See also
canConvert(), convert()

Definition at line 1768 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toJsonObject()

QJsonObject QVariant::toJsonObject ( ) const
Since
5.0

Returns the variant as a QJsonObject if the variant has userType() \l QJsonObject; otherwise returns a default constructed QJsonObject.

See also
canConvert(), convert()

Definition at line 1742 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toJsonValue()

QJsonValue QVariant::toJsonValue ( ) const
Since
5.0

Returns the variant as a QJsonValue if the variant has userType() \l QJsonValue; otherwise returns a default constructed QJsonValue.

See also
canConvert(), convert()

Definition at line 1729 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toLine()

QLine QVariant::toLine ( ) const

Returns the variant as a QLine if the variant has userType() \l QMetaType::QLine; otherwise returns an invalid QLine.

See also
canConvert(), convert()

Definition at line 1613 of file qvariant.cpp.

◆ toLineF()

QLineF QVariant::toLineF ( ) const

Returns the variant as a QLineF if the variant has userType() \l QMetaType::QLineF; otherwise returns an invalid QLineF.

See also
canConvert(), convert()

Definition at line 1600 of file qvariant.cpp.

◆ toList()

QVariantList QVariant::toList ( ) const

Returns the variant as a QVariantList if the variant has userType() \l QMetaType::QVariantList or \l QMetaType::QStringList; otherwise returns an empty list.

See also
canConvert(), convert()

Definition at line 1979 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toLocale()

QLocale QVariant::toLocale ( ) const

Returns the variant as a QLocale if the variant has userType() \l QMetaType::QLocale; otherwise returns an invalid QLocale.

See also
canConvert(), convert()

Definition at line 1657 of file qvariant.cpp.

◆ toLongLong()

qlonglong QVariant::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.

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(), convert()

Definition at line 1872 of file qvariant.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toMap()

QVariantMap QVariant::toMap ( ) const

Returns the variant as a QMap<QString, QVariant> if the variant has type() \l QMetaType::QVariantMap; otherwise returns an empty map.

See also
canConvert(), convert()

Definition at line 1427 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toPoint()

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.

See also
canConvert(), convert()

Definition at line 1534 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toPointF()

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.

See also
canConvert(), convert()

Definition at line 1627 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toReal()

qreal QVariant::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.

Since
4.6

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(), convert()

Definition at line 1967 of file qvariant.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toRect()

QRect QVariant::toRect ( ) const

Returns the variant as a QRect if the variant has userType() \l QMetaType::QRect; otherwise returns an invalid QRect.

See also
canConvert(), convert()

Definition at line 1547 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toRectF()

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.

See also
canConvert(), convert()

Definition at line 1587 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toSize()

QSize QVariant::toSize ( ) const

Returns the variant as a QSize if the variant has userType() \l QMetaType::QSize; otherwise returns an invalid QSize.

See also
canConvert(), convert()

Definition at line 1560 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toSizeF()

QSizeF QVariant::toSizeF ( ) const

Returns the variant as a QSizeF if the variant has userType() \l QMetaType::QSizeF; otherwise returns an invalid QSizeF.

See also
canConvert(), convert()

Definition at line 1573 of file qvariant.cpp.

◆ toString()

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.

See also
canConvert(), convert()

Definition at line 1416 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toStringList()

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.

See also
canConvert(), convert()

Definition at line 1396 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toTime()

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 type() is \l QMetaType::QString, an invalid time will be returned if the string cannot be parsed as a Qt::ISODate format time.

See also
canConvert(), convert()

Definition at line 1472 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toUInt()

uint QVariant::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.

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

See also
canConvert(), convert()

Definition at line 1855 of file qvariant.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toULongLong()

qulonglong QVariant::toULongLong ( bool *  ok = nullptr) const

Returns the variant as an unsigned long long int if the variant has type() \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.

See also
canConvert(), convert()

Definition at line 1889 of file qvariant.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toUrl()

QUrl QVariant::toUrl ( ) const

Returns the variant as a QUrl if the variant has userType() \l QMetaType::QUrl; otherwise returns an invalid QUrl.

See also
canConvert(), convert()

Definition at line 1643 of file qvariant.cpp.

Here is the caller graph for this function:

◆ toUuid()

QUuid QVariant::toUuid ( ) const
Since
5.0

Returns the variant as a QUuid if the variant has type() \l QMetaType::QUuid, \l QMetaType::QByteArray or \l QMetaType::QString; otherwise returns a default-constructed QUuid.

See also
canConvert(), convert()

Definition at line 1715 of file qvariant.cpp.

Here is the caller graph for this function:

◆ typeId()

int QVariant::typeId ( ) const
inline

Returns the storage type of the value stored in the variant. This is the same as metaType().id().

See also
metaType()

Definition at line 241 of file qvariant.h.

Here is the caller graph for this function:

◆ typeName()

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.

Definition at line 1073 of file qvariant.cpp.

Here is the call graph for this function:

◆ userType()

int QVariant::userType ( ) const
inline

Definition at line 240 of file qvariant.h.

Here is the caller graph for this function:

◆ value()

template<typename T >
template< typename T > T QVariant::value ( ) const
inline

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, a \l{default-constructed value} will be returned.

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

Example:

If the QVariant contains a pointer to a type derived from QObject then {T} may be any QObject type. If the pointer stored in the QVariant can be qobject_cast to T, then that result is returned. Otherwise \nullptr is returned. Note that this only works for QObject subclasses which use the Q_OBJECT macro.

If the QVariant contains a sequential container and {T} is QVariantList, the elements of the container will be converted into \l {QVariant}s and returned as a QVariantList.

See also
setValue(), fromValue(), canConvert(), Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE()

Definition at line 378 of file qvariant.h.

Here is the caller graph for this function:

◆ view() [1/2]

template<typename T >
template< typename T > T QVariant::view ( )
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.

See also
canView(), Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE()

Definition at line 382 of file qvariant.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ view() [2/2]

bool QVariant::view ( int  type,
void ptr 
)
protected

Definition at line 2083 of file qvariant.cpp.

Friends And Related Function Documentation

◆ operator!=

bool QVariant::operator!= ( const QVariant v1,
const QVariant v2 
)
friend

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

QVariant uses the equality operator of the type() contained to check for equality.

Variants of different types will always compare as not equal with a few exceptions:

\list

  • If both types are numeric types (integers and floatins point numbers) Qt will compare those types using standard C++ type promotion rules.
  • If one type is numeric and the other one a QString, Qt will try to convert the QString to a matching numeric type and if successful compare those.
  • If both variants contain pointers to QObject derived types, QVariant will check whether the types are related and point to the same object. \endlist

Definition at line 521 of file qvariant.h.

◆ operator<<

template<typename T >
auto operator<< ( const QDebug debug,
const T variant 
) -> std::enable_if_t<std::is_same_v<T, QVariant>, QDebug>
friend

Definition at line 525 of file qvariant.h.

◆ operator==

bool QVariant::operator== ( const QVariant v1,
const QVariant v2 
)
friend

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

QVariant uses the equality operator of the type() contained to check for equality.

Variants of different types will always compare as not equal with a few exceptions:

\list

  • If both types are numeric types (integers and floatins point numbers) Qt will compare those types using standard C++ type promotion rules.
  • If one type is numeric and the other one a QString, Qt will try to convert the QString to a matching numeric type and if successful compare those.
  • If both variants contain pointers to QObject derived types, QVariant will check whether the types are related and point to the same object. \endlist

The result of the function is not affected by the result of QVariant::isNull, which means that two values can be equal even if one of them is null and another is not.

Definition at line 519 of file qvariant.h.

◆ qvariant_cast

template<typename T >
template< typename T > T qvariant_cast ( const QVariant value)
friend

Returns the given value converted to the template type {T}.

This function is equivalent to QVariant::value().

See also
QVariant::value()

Definition at line 620 of file qvariant.h.

◆ qVariantCanConvert()

bool qVariantCanConvert ( const QVariant value)
related
Deprecated:

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

Note
This function was provided as a workaround for MSVC 6 which did not support member template functions. It is advised to use the other form in new code.
See also
QVariant::canConvert()

◆ QVariantHash()

QVariantHash
related
Since
4.5

Synonym for QHash<QString, QVariant>.

Definition at line 76 of file qcontainerfwd.h.

◆ QVariantList()

QVariantList
related

Synonym for QList<QVariant>.

Definition at line 51 of file qjsonarray.h.

◆ QVariantMap()

QVariantMap
related

Synonym for QMap<QString, QVariant>.

Definition at line 75 of file qcontainerfwd.h.

◆ qVariantValue()

template< typename T > T qVariantValue ( const QVariant value)
related
Deprecated:

Returns the given value converted to the template type {T}.

This function is equivalent to \l{QVariant::value()}{QVariant::value}<T>(value).

Note
This function was provided as a workaround for MSVC 6 which did not support member template functions. It is advised to use the other form in new code.
See also
QVariant::value(), qvariant_cast()

Member Data Documentation

◆ d

Private QVariant::d
protected

Definition at line 533 of file qvariant.h.


The documentation for this class was generated from the following files: