QVariant 類舉動像最常見 Qt 數據類型的並集。 更多...
| 頭: |
#include <QVariant>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
|
| qmake: |
QT += core
|
此類 相等可比較 .
| QVariant () | |
| QVariant (QChar c ) | |
| QVariant (const char * val ) | |
| QVariant (double val ) | |
| QVariant (float val ) | |
| QVariant (int val ) | |
| QVariant (qlonglong val ) | |
| QVariant (qulonglong val ) | |
| QVariant (uint val ) | |
| QVariant (QMetaType type , const void * copy = nullptr) | |
(從 6.6 起)
|
QVariant (std::in_place_type_t<T>, Args &&... args ) |
(從 6.6 起)
|
QVariant (std::in_place_type_t<T>, std::initializer_list<U> il , Args &&... args ) |
| QVariant (QDate val ) | |
| QVariant (QLatin1StringView val ) | |
| QVariant (QLine val ) | |
| QVariant (QLineF val ) | |
| QVariant (QPoint val ) | |
| QVariant (QPointF val ) | |
| QVariant (QRect val ) | |
| QVariant (QRectF val ) | |
| QVariant (QSize val ) | |
| QVariant (QSizeF val ) | |
| QVariant (QTime val ) | |
| QVariant (QUuid val ) | |
| QVariant (bool val ) | |
| QVariant (const QBitArray & val ) | |
| QVariant (const QByteArray & val ) | |
| QVariant (const QDateTime & val ) | |
| QVariant (const QEasingCurve & val ) | |
| QVariant (const QHash<QString, QVariant> & val ) | |
| QVariant (const QJsonArray & val ) | |
| QVariant (const QJsonDocument & val ) | |
| QVariant (const QJsonObject & val ) | |
| QVariant (const QJsonValue & val ) | |
| QVariant (const QList<QVariant> & val ) | |
| QVariant (const QLocale & l ) | |
| QVariant (const QMap<QString, QVariant> & val ) | |
| QVariant (const QModelIndex & val ) | |
| QVariant (const QPersistentModelIndex & val ) | |
| QVariant (const QRegularExpression & re ) | |
| QVariant (const QString & val ) | |
| QVariant (const QStringList & val ) | |
| QVariant (const QUrl & val ) | |
| QVariant (const QVariant & p ) | |
| QVariant (QVariant && other ) | |
| ~QVariant () | |
| bool | canConvert () const |
(從 6.0 起)
bool
|
canConvert (QMetaType type ) const |
| bool | canView () const |
| void | clear () |
| const void * | constData () const |
(從 6.0 起)
bool
|
convert (QMetaType targetType ) |
| void * | data () |
| const void * | data () const |
(從 6.6 起)
T &
|
emplace (Args &&... args ) |
(從 6.6 起)
T &
|
emplace (std::initializer_list<U> list , Args &&... args ) |
| bool | isNull () const |
| bool | isValid () const |
(從 6.0 起)
QMetaType
|
metaType () const |
| void | setValue (QVariant && value ) |
| void | setValue (T && value ) |
| void | setValue (const QVariant & value ) |
| void | swap (QVariant & other ) |
| QBitArray | toBitArray () const |
| bool | toBool () const |
| QByteArray | toByteArray () const |
| QChar | toChar () const |
| QDate | toDate () const |
| QDateTime | toDateTime () const |
| double | toDouble (bool * ok = nullptr) const |
| QEasingCurve | toEasingCurve () const |
| float | toFloat (bool * ok = nullptr) const |
| QHash<QString, QVariant> | toHash () const |
| int | toInt (bool * ok = nullptr) const |
| QJsonArray | toJsonArray () const |
| QJsonDocument | toJsonDocument () const |
| QJsonObject | toJsonObject () const |
| QJsonValue | toJsonValue () const |
| QLine | toLine () const |
| QLineF | toLineF () const |
| QList<QVariant> | toList () const |
| QLocale | toLocale () const |
| qlonglong | toLongLong (bool * ok = nullptr) const |
| QMap<QString, QVariant> | toMap () const |
| QModelIndex | toModelIndex () const |
| QPersistentModelIndex | toPersistentModelIndex () const |
| QPoint | toPoint () const |
| QPointF | toPointF () const |
| qreal | toReal (bool * ok = nullptr) const |
| QRect | toRect () const |
| QRectF | toRectF () const |
| QRegularExpression | toRegularExpression () const |
| QSize | toSize () const |
| QSizeF | toSizeF () const |
| QString | toString () const |
| QStringList | toStringList () const |
| QTime | toTime () const |
| uint | toUInt (bool * ok = nullptr) const |
| qulonglong | toULongLong (bool * ok = nullptr) const |
| QUrl | toUrl () const |
| QUuid | toUuid () const |
| int | typeId () const |
| const char * | typeName () const |
| int | userType () const |
| T | value () const & |
| T | view () |
| QVariant & | operator= (QVariant && other ) |
| QVariant & | operator= (const QVariant & variant ) |
(從 6.0 起)
QPartialOrdering
|
compare (const QVariant & lhs , const QVariant & rhs ) |
(從 6.7 起)
QVariant
|
fromMetaType (QMetaType type , const void * copy = nullptr) |
| QVariant | fromStdVariant (const std::variant<Types...> & value ) |
(從 6.6 起)
QVariant
|
fromStdVariant (std::variant<Types...> && value ) |
| QVariant | fromValue (const T & value ) |
(從 6.6 起)
QVariant
|
fromValue (T && value ) |
| QVariantHash | |
| QVariantList | |
| QVariantMap | |
(從 6.6 起)
T &
|
get (QVariant & v ) |
(從 6.6 起)
T &&
|
get (QVariant && v ) |
(從 6.6 起)
const T &
|
get (const QVariant & v ) |
(從 6.6 起)
const T &&
|
get (const QVariant && v ) |
(從 6.6 起)
T *
|
get_if (QVariant * v ) |
(從 6.6 起)
const T *
|
get_if (const QVariant * v ) |
| T | qvariant_cast (const QVariant & value ) |
(從 6.7 起)
T
|
qvariant_cast (QVariant && value ) |
| bool | operator!= (const QVariant & lhs , const QVariant & rhs ) |
| QDataStream & | operator<< (QDataStream & s , const QVariant & p ) |
| bool | operator== (const QVariant & lhs , const QVariant & rhs ) |
| QDataStream & | operator>> (QDataStream & s , QVariant & p ) |
QVariant 對象保持單個值為單個 typeId () 每次。(某些類型是多值的,例如字符串列錶) 可以找齣由變體保持的 T 類型,將其轉換為不同類型使用 convert (),使用某一 toT() 函數獲取其值 (如 toSize ()),和校驗類型是否可以被轉換成特定類型使用 canConvert ().
方法命名 toT() (如: toInt (), toString ()) 為 const。若要求存儲類型,它們返迴存儲對象的副本。若要求可以從存儲類型生成的類型,toT() 拷貝-轉換並保持對象本身不變。若要求無法從存儲類型生成的類型,結果從屬類型;見函數文檔編製瞭解細節。
這裏是演示 QVariant 用法的一些範例代碼:
QDataStream out(...); QVariant v(123); // The variant now contains an int int x = v.toInt(); // x = 123 out << v; // Writes a type tag and an int to out v = QVariant(tr("hello")); // The variant now contains a QString int y = v.toInt(); // y = 0 since v cannot be converted to an int QString s = v.toString(); // s = tr("hello") (see QObject::tr()) out << v; // Writes a type tag and a QString to out ... QDataStream in(...); // (opening the previously written stream) in >> v; // Reads an Int variant int z = v.toInt(); // z = 123 qDebug("Type is %s", // prints "Type is int" v.typeName()); v = v.toInt() + 100; // The variant now holds the value 223 v = QVariant(QStringList()); // The variant now holds a QStringList
甚至可以存儲 QList <QVariant> 和 QMap < QString ,QVariant> 值在變體中,因此可以輕鬆構造任意類型的任何復雜數據結構。這是非常強大且萬能,但可能證明內存更少且速度比在標準數據結構中存儲特定類型更快。
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 x; // x.isNull() == true QVariant y = QVariant::fromValue(nullptr); // y.isNull() == true
QVariant can be extended to support other types than those mentioned in the QMetaType::Type 枚舉。見 創建自定義 Qt 類型 瞭解細節。
因為 QVariant 是 Qt Core 模塊的一部分,所以它無法提供對 Qt GUI 中定義的數據類型的轉換函數,譬如
QColor
,
QImage
,和
QPixmap
。換句話說,沒有
toColor()
函數。取而代之,可以使用
QVariant::value
() 或
qvariant_cast
() 模闆函數。例如:
QVariant variant; ... QColor color = variant.value<QColor>();
反嚮轉換 (如,從 QColor to QVariant) is automatic for all data types supported by QVariant, including GUI-related types:
QColor color = palette().background().color(); QVariant variant = color;
當使用 canConvert () 和 convert () 連續,它是可能的對於 canConvert () 會返迴 true,但 convert () 會返迴 false。通常,這是因為 canConvert () 僅報告 QVariant 在給定閤適數據類型之間轉換的一般能力;提供實際無法轉換的數據,仍是可能的。
例如,
canConvert(QMetaType::fromType<int>())
would return true when called on a variant containing a string because, in principle, QVariant is able to convert strings of numbers to integers. However, if the string contains non-numeric characters, it cannot be converted to an integer, and any attempt to convert it will fail. Hence, it is important to have both functions return true for a successful conversion.
另請參閱 QMetaType .
Returns the storage type of the value stored in the variant. This is the same as metaType ().id().
另請參閱 metaType ().
Returns a pointer to the contained object as a generic void* that cannot be written to.
[noexcept]
QVariant::
QVariant
()
構造無效變體。
[noexcept]
QVariant::
QVariant
(
QChar
c
)
Constructs a new variant with a char value, c .
Constructs a new variant with a string value of val . The variant creates a deep copy of val 成 QString assuming UTF-8 encoding on the input val .
注意, val is converted to a QString for storing in the variant and QVariant::userType () 會返迴 QMetaType::QString for the variant.
可以禁用此運算符通過定義
QT_NO_CAST_FROM_ASCII
當編譯應用程序時。
[noexcept]
QVariant::
QVariant
(
double
val
)
Constructs a new variant with a floating point value, val .
[noexcept]
QVariant::
QVariant
(
float
val
)
Constructs a new variant with a floating point value, val .
[noexcept]
QVariant::
QVariant
(
int
val
)
Constructs a new variant with an integer value, val .
[noexcept]
QVariant::
QVariant
(
qlonglong
val
)
Constructs a new variant with a long long integer value, val .
[noexcept]
QVariant::
QVariant
(
qulonglong
val
)
Constructs a new variant with an unsigned long long integer value, val .
[noexcept]
QVariant::
QVariant
(
uint
val
)
Constructs a new variant with an unsigned integer value, val .
[explicit]
QVariant::
QVariant
(
QMetaType
type
, const
void
*
copy
= nullptr)
Constructs a variant of type
type
, and initializes it with a copy of
*copy
if
copy
不是
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
,和
QMetaType::QObjectStar
.
若
type
does not support copy construction and
copy
不是
nullptr
, the variant will be invalid. Similarly, if
copy
is
nullptr
and
type
does not support default construction, the variant will be invalid.
另請參閱 QVariant::fromMetaType , QVariant::fromValue (),和 QMetaType::Type .
[explicit noexcept(...), since 6.6]
template <typename T, typename... Args, QVariant::if_constructible<T, Args...> = true> QVariant::
QVariant
(
std::in_place_type_t
<
T
>,
Args
&&...
args
)
Constructs a new variant containing a value of type
T
. The contained value is initialized with the arguments
std::forward<Args>(args)...
.
This overload only participates in overload resolution if
T
can be constructed from
args
.
This constructor is provided for STL/std::any compatibility.
這是重載函數。
該函數在 Qt 6.6 引入。
注意:
This function is noexcept when
is_noexcept_constructible<q20::remove_cvref_t<T>, Args...>::value
is
true
.
[explicit noexcept(...), since 6.6]
template <typename T, typename U, typename... Args, QVariant::if_constructible<T, std::initializer_list<U> &, Args...> = true> QVariant::
QVariant
(
std::in_place_type_t
<
T
>,
std::initializer_list
<
U
>
il
,
Args
&&...
args
)
這是重載函數。
This overload exists to support types with constructors taking an
initializer_list
. It behaves otherwise equivalent to the non-initializer list
in_place_type_t
overload.
該函數在 Qt 6.6 引入。
注意:
This function is noexcept when
is_noexcept_constructible<q20::remove_cvref_t<T>,std::initializer_list<U> &,Args...>::value
is
true
.
[noexcept]
QVariant::
QVariant
(
QDate
val
)
Constructs a new variant with a date value, val .
構造新變體采用 QString value from the Latin-1 string viewed by val .
[noexcept(...)]
QVariant::
QVariant
(
QLine
val
)
Constructs a new variant with a line value of val .
注意:
This function is noexcept when
Private::FitsInInternalSize<sizeof(int) * 4>
is
true
.
[noexcept(...)]
QVariant::
QVariant
(
QLineF
val
)
Constructs a new variant with a line value of val .
注意:
This function is noexcept when
Private::FitsInInternalSize<sizeof(qreal) * 4>
is
true
.
[noexcept]
QVariant::
QVariant
(
QPoint
val
)
Constructs a new variant with a point value of val .
[noexcept(...)]
QVariant::
QVariant
(
QPointF
val
)
Constructs a new variant with a point value of val .
注意:
This function is noexcept when
Private::FitsInInternalSize<sizeof(qreal) * 2>
is
true
.
[noexcept(...)]
QVariant::
QVariant
(
QRect
val
)
Constructs a new variant with a rect value of val .
注意:
This function is noexcept when
Private::FitsInInternalSize<sizeof(int) * 4>
is
true
.
[noexcept(...)]
QVariant::
QVariant
(
QRectF
val
)
Constructs a new variant with a rect value of val .
注意:
This function is noexcept when
Private::FitsInInternalSize<sizeof(qreal) * 4>
is
true
.
[noexcept]
QVariant::
QVariant
(
QSize
val
)
Constructs a new variant with a size value of val .
[noexcept(...)]
QVariant::
QVariant
(
QSizeF
val
)
Constructs a new variant with a size value of val .
注意:
This function is noexcept when
Private::FitsInInternalSize<sizeof(qreal) * 2>
is
true
.
[noexcept]
QVariant::
QVariant
(
QTime
val
)
Constructs a new variant with a time value, val .
[noexcept(...)]
QVariant::
QVariant
(
QUuid
val
)
構造新變體采用 UUID (通用唯一標識符) 值 val .
注意:
This function is noexcept when
Private::FitsInInternalSize<16>
is
true
.
[noexcept]
QVariant::
QVariant
(
bool
val
)
Constructs a new variant with a boolean value, val .
[noexcept]
QVariant::
QVariant
(const
QBitArray
&
val
)
Constructs a new variant with a bitarray value, val .
[noexcept]
QVariant::
QVariant
(const
QByteArray
&
val
)
Constructs a new variant with a bytearray value, val .
[noexcept]
QVariant::
QVariant
(const
QDateTime
&
val
)
Constructs a new variant with a date/time value, val .
構造新變體采用緩和麯綫值 val .
[noexcept]
QVariant::
QVariant
(const
QHash
<
QString
,
QVariant
> &
val
)
Constructs a new variant with a hash of QVariant s, val .
[noexcept]
QVariant::
QVariant
(const
QJsonArray
&
val
)
構造新變體采用 JSON 數組值 val .
構造新變體采用 JSON 文檔值 val .
[noexcept]
QVariant::
QVariant
(const
QJsonObject
&
val
)
構造新變體采用 JSON 對象值 val .
[noexcept(...)]
QVariant::
QVariant
(const
QJsonValue
&
val
)
構造新變體采用 JSON 值 val .
注意:
This function is noexcept when
Private::FitsInInternalSize<sizeof(CborValueStandIn)>
is
true
.
[noexcept]
QVariant::
QVariant
(const
QList
<
QVariant
> &
val
)
Constructs a new variant with a list value, val .
[noexcept]
QVariant::
QVariant
(const
QLocale
&
l
)
構造新變體采用區域設置值 l .
[noexcept]
QVariant::
QVariant
(const
QMap
<
QString
,
QVariant
> &
val
)
Constructs a new variant with a map of QVariant s, val .
[noexcept(...)]
QVariant::
QVariant
(const
QModelIndex
&
val
)
構造新變體采用 QModelIndex 值, val .
注意:
This function is noexcept when
Private::FitsInInternalSize<8 + 2 * sizeof(quintptr)>
is
true
.
構造新變體采用 QPersistentModelIndex 值, val .
[noexcept]
QVariant::
QVariant
(const
QRegularExpression
&
re
)
構造新變體采用正則錶達式值 re .
[noexcept]
QVariant::
QVariant
(const
QString
&
val
)
Constructs a new variant with a string value, val .
[noexcept]
QVariant::
QVariant
(const
QStringList
&
val
)
Constructs a new variant with a string list value, val .
[noexcept]
QVariant::
QVariant
(const
QUrl
&
val
)
構造新變體采用 URL 值 val .
Constructs a copy of the variant, p , passed as the argument to this constructor.
[noexcept]
QVariant::
QVariant
(
QVariant
&&
other
)
移動構造 QVariant 實例,使之指嚮同一對象如 other 所指嚮的。
[noexcept]
QVariant::
~QVariant
()
銷毀 QVariant 和包含對象。
返迴
true
若可以將變體轉換成模闆類型
T
,否則 false。
範例:
QVariant v = 42; v.canConvert<int>(); // returns true v.canConvert<QString>(); // returns true MyCustomStruct s; v.setValue(s); v.canConvert<int>(); // returns false v.canConvert<MyCustomStruct>(); // returns true
A
QVariant
包含的類型指針派生自
QObject
也會返迴 true 對於此函數若
qobject_cast
到模闆類型
T
would succeed. Note that this only works for
QObject
subclasses which use the
Q_OBJECT
宏。
另請參閱 convert ().
[since 6.0]
bool
QVariant::
canConvert
(
QMetaType
type
) const
返迴
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.
該函數在 Qt 6.0 引入。
另請參閱 QMetaType::canConvert ().
返迴
true
if a mutable view of the template type
T
can be created on this variant, otherwise
false
.
另請參閱 value ().
將此變體轉換成類型 QMetaType::UnknownType 並釋放任何使用資源。
[static, since 6.0]
QPartialOrdering
QVariant::
compare
(const
QVariant
&
lhs
, const
QVariant
&
rhs
)
比較對象在 lhs and rhs for ordering.
返迴 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 ,分彆。
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 a numeric comparison is done and at least one value is NaN, QPartialOrdering::Unordered 被返迴。
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.
該函數在 Qt 6.0 引入。
另請參閱 QMetaType::compare () 和 QMetaType::isOrdered ().
[since 6.0]
bool
QVariant::
convert
(
QMetaType
targetType
)
將變體鑄造成請求類型, 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).
返迴
true
if the current type of the variant was successfully cast; otherwise returns
false
.
A QVariant 包含的類型指針派生自 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 宏。
注意:
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
.
該函數在 Qt 6.0 引入。
另請參閱 canConvert () 和 clear ().
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 null-QVariant , QVariant will not be null after the call.
[since 6.6]
template <typename T, typename... Args, QVariant::if_constructible<T, Args...> = true>
T
&QVariant::
emplace
(
Args
&&...
args
)
Replaces the object currently held in
*this
with an object of type
T
, constructed from
args
...
。若
*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.
該函數在 Qt 6.6 引入。
[since 6.6]
template <typename T, typename U, typename... Args, QVariant::if_constructible<T, std::initializer_list<U> &, Args...> = true>
T
&QVariant::
emplace
(
std::initializer_list
<
U
>
list
,
Args
&&...
args
)
這是重載函數。
This overload exists to support types with constructors taking an
initializer_list
. It behaves otherwise equivalent to the non-initializer list overload.
該函數在 Qt 6.6 引入。
[static, since 6.7]
QVariant
QVariant::
fromMetaType
(
QMetaType
type
, const
void
*
copy
= nullptr)
Creates a variant of type
type
, and initializes it with a copy of
*copy
if
copy
不是
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
,和
QMetaType::QObjectStar
.
若
type
does not support copy construction and
copy
不是
nullptr
, the variant will be invalid. Similarly, if
copy
is
nullptr
and
type
does not support default construction, the variant will be invalid.
返迴 QVariant created as described above.
該函數在 Qt 6.7 引入。
另請參閱 QVariant::fromValue () 和 QMetaType::Type .
[static]
template <typename... Types>
QVariant
QVariant::
fromStdVariant
(const
std::variant
<
類型
...> &
value
)
返迴 QVariant with the type and value of the active variant of value . If the active type is std::monostate a default QVariant 被返迴。
注意: 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.
另請參閱 fromValue ().
[static, since 6.6]
template <typename... Types>
QVariant
QVariant::
fromStdVariant
(
std::variant
<
類型
...> &&
value
)
這是重載函數。
該函數在 Qt 6.6 引入。
[static]
template <typename T>
QVariant
QVariant::
fromValue
(const
T
&
value
)
返迴 QVariant containing a copy of value . Behaves exactly like setValue () otherwise.
範例:
MyCustomStruct s; return QVariant::fromValue(s);
[static, since 6.6]
template <typename T, QVariant::if_rvalue<T> = true>
QVariant
QVariant::
fromValue
(
T
&&
value
)
這是重載函數。
該函數在 Qt 6.6 引入。
返迴
true
if this is a null variant, false otherwise.
A variant is considered null if it contains no initialized value or a null pointer.
注意: 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.
另請參閱 convert ().
返迴
true
若此變體的存儲類型不是
QMetaType::UnknownType
;否則返迴
false
.
[since 6.0]
QMetaType
QVariant::
metaType
() const
返迴 QMetaType of the value stored in the variant.
該函數在 Qt 6.0 引入。
移動 value over this QVariant . It is equivalent to simply move assigning value 到此 QVariant .
另請參閱 value ().
Stores a copy of
value
。若
T
is a type that
QVariant
不支持,
QMetaType
is used to store the value. A compile error will occur if
QMetaType
doesn't handle the type.
範例:
QVariant v; v.setValue(5); int i = v.toInt(); // i is now 5 QString s = v.toString(); // s is now "5" MyCustomStruct c; v.setValue(c); ... MyCustomStruct c2 = v.value<MyCustomStruct>();
另請參閱 value (), fromValue (),和 canConvert ().
拷貝 value over this QVariant . It is equivalent to simply assigning value 到此 QVariant .
[noexcept]
void
QVariant::
swap
(
QVariant
&
other
)
Swaps this variant with other 。此操作很快且從不失敗。
返迴變體如 QBitArray 若變體擁有 userType () QMetaType::QBitArray ;否則返迴空的位數組。
另請參閱 canConvert () 和 convert ().
Returns the variant as a bool if the variant has userType () Bool.
返迴
true
若變體擁有
userType
()
QMetaType::Bool
,
QMetaType::QChar
,
QMetaType::Double
,
QMetaType::Int
,
QMetaType::LongLong
,
QMetaType::UInt
,或
QMetaType::ULongLong
and the value is non-zero, or if the variant has type
QMetaType::QString
or
QMetaType::QByteArray
and its lower-case content is not one of the following: empty, "0" or "false"; otherwise returns
false
.
另請參閱 canConvert () 和 convert ().
返迴變體如 QByteArray 若變體擁有 userType () QMetaType::QByteArray or QMetaType::QString (轉換使用 QString::fromUtf8 ());否則返迴空字節數組。
另請參閱 canConvert () 和 convert ().
返迴變體如 QChar 若變體擁有 userType () QMetaType::QChar , QMetaType::Int ,或 QMetaType::UInt ;否則返迴無效 QChar .
另請參閱 canConvert () 和 convert ().
返迴變體如 QDate 若變體擁有 userType () QMetaType::QDate , QMetaType::QDateTime ,或 QMetaType::QString ; otherwise returns an invalid date.
If the type() is QMetaType::QString , an invalid date will be returned if the string cannot be parsed as a Qt::ISODate format date.
另請參閱 canConvert () 和 convert ().
返迴變體如 QDateTime 若變體擁有 userType () QMetaType::QDateTime , QMetaType::QDate ,或 QMetaType::QString ;否則返迴無效日期/時間。
If the type() is QMetaType::QString , an invalid date/time will be returned if the string cannot be parsed as a Qt::ISODate format date/time.
另請參閱 canConvert () 和 convert ().
Returns the variant as a double if the variant has userType () QMetaType::Double , QMetaType::Float , QMetaType::Bool , QMetaType::QByteArray , QMetaType::Int , QMetaType::LongLong , QMetaType::QString , QMetaType::UInt ,或 QMetaType::ULongLong ;否則返迴 0.0。
若
ok
非 null:
*
ok
is set to true if the value could be converted to a double; otherwise
*
ok
被設為 false。
另請參閱 canConvert () 和 convert ().
返迴變體如 QEasingCurve 若變體擁有 userType () QMetaType::QEasingCurve ; otherwise returns a default easing curve.
另請參閱 canConvert () 和 convert ().
Returns the variant as a float if the variant has userType () QMetaType::Double , QMetaType::Float , QMetaType::Bool , QMetaType::QByteArray , QMetaType::Int , QMetaType::LongLong , QMetaType::QString , QMetaType::UInt ,或 QMetaType::ULongLong ;否則返迴 0.0。
若
ok
非 null:
*
ok
is set to true if the value could be converted to a double; otherwise
*
ok
被設為 false。
另請參閱 canConvert () 和 convert ().
返迴變體如 QHash < QString , QVariant > if the variant has type() QMetaType::QVariantHash ; otherwise returns an empty map.
另請參閱 canConvert () 和 convert ().
Returns the variant as an int if the variant has userType () QMetaType::Int , QMetaType::Bool , QMetaType::QByteArray , QMetaType::QChar , QMetaType::Double , QMetaType::LongLong , QMetaType::QString , QMetaType::UInt ,或 QMetaType::ULongLong ;否則返迴 0。
若
ok
非 null:
*
ok
is set to true if the value could be converted to an int; otherwise
*
ok
被設為 false。
警告: If the value is convertible to a 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 ().
另請參閱 canConvert () 和 convert ().
返迴變體如 QJsonArray 若變體擁有 userType () QJsonArray ;否則返迴默認構造的 QJsonArray .
另請參閱 canConvert () 和 convert ().
返迴變體如 QJsonDocument 若變體擁有 userType () QJsonDocument ;否則返迴默認構造的 QJsonDocument .
另請參閱 canConvert () 和 convert ().
返迴變體如 QJsonObject 若變體擁有 userType () QJsonObject ;否則返迴默認構造的 QJsonObject .
另請參閱 canConvert () 和 convert ().
返迴變體如 QJsonValue 若變體擁有 userType () QJsonValue ;否則返迴默認構造的 QJsonValue .
另請參閱 canConvert () 和 convert ().
返迴變體如 QLine 若變體擁有 userType () QMetaType::QLine ;否則返迴無效 QLine .
另請參閱 canConvert () 和 convert ().
返迴變體如 QLineF 若變體擁有 userType () QMetaType::QLineF ;否則返迴無效 QLineF .
另請參閱 canConvert () 和 convert ().
返迴變體如 QVariantList 若變體擁有 userType () 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 Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE . If none of those conditions are true, this function will return an empty list.
另請參閱 canConvert () 和 convert ().
返迴變體如 QLocale 若變體擁有 userType () QMetaType::QLocale ;否則返迴無效 QLocale .
另請參閱 canConvert () 和 convert ().
Returns the variant as a long long int if the variant has userType () QMetaType::LongLong , QMetaType::Bool , QMetaType::QByteArray , QMetaType::QChar , QMetaType::Double , QMetaType::Int , QMetaType::QString , QMetaType::UInt ,或 QMetaType::ULongLong ;否則返迴 0。
若
ok
非 null:
*
ok
is set to true if the value could be converted to an int; otherwise
*
ok
被設為 false。
另請參閱 canConvert () 和 convert ().
返迴變體如 QVariantMap if the variant has type() 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 Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE . If none of those conditions are true, this function will return an empty map.
另請參閱 canConvert () 和 convert ().
返迴變體如 QModelIndex 若變體擁有 userType () QModelIndex ;否則返迴默認構造的 QModelIndex .
另請參閱 canConvert (), convert (),和 toPersistentModelIndex ().
返迴變體如 QPersistentModelIndex 若變體擁有 userType () QPersistentModelIndex ;否則返迴默認構造的 QPersistentModelIndex .
另請參閱 canConvert (), convert (),和 toModelIndex ().
返迴變體如 QPoint 若變體擁有 userType () QMetaType::QPoint or QMetaType::QPointF ; otherwise returns a null QPoint .
另請參閱 canConvert () 和 convert ().
返迴變體如 QPointF 若變體擁有 userType () QMetaType::QPoint or QMetaType::QPointF ; otherwise returns a null QPointF .
另請參閱 canConvert () 和 convert ().
Returns the variant as a qreal if the variant has userType () QMetaType::Double , QMetaType::Float , QMetaType::Bool , QMetaType::QByteArray , QMetaType::Int , QMetaType::LongLong , QMetaType::QString , QMetaType::UInt ,或 QMetaType::ULongLong ;否則返迴 0.0。
若
ok
非 null:
*
ok
is set to true if the value could be converted to a double; otherwise
*
ok
被設為 false。
另請參閱 canConvert () 和 convert ().
返迴變體如 QRect 若變體擁有 userType () QMetaType::QRect ;否則返迴無效 QRect .
另請參閱 canConvert () 和 convert ().
返迴變體如 QRectF 若變體擁有 userType () QMetaType::QRect or QMetaType::QRectF ;否則返迴無效 QRectF .
另請參閱 canConvert () 和 convert ().
返迴變體如 QRegularExpression 若變體擁有 userType () QRegularExpression ;否則返迴空 QRegularExpression .
另請參閱 canConvert () 和 convert ().
返迴變體如 QSize 若變體擁有 userType () QMetaType::QSize ;否則返迴無效 QSize .
另請參閱 canConvert () 和 convert ().
返迴變體如 QSizeF 若變體擁有 userType () QMetaType::QSizeF ;否則返迴無效 QSizeF .
另請參閱 canConvert () 和 convert ().
返迴變體如 QString if the variant has a userType () 包括,但不限於:
QMetaType::QString , QMetaType::Bool , QMetaType::QByteArray , QMetaType::QChar , QMetaType::QDate , QMetaType::QDateTime , QMetaType::Double , QMetaType::Int , QMetaType::LongLong , QMetaType::QStringList , QMetaType::QTime , QMetaType::UInt ,或 QMetaType::ULongLong .
Calling QVariant::toString() on an unsupported variant returns an empty string.
另請參閱 canConvert () 和 convert ().
返迴變體如 QStringList 若變體擁有 userType () QMetaType::QStringList , QMetaType::QString ,或 QMetaType::QVariantList of a type that can be converted to QString ; otherwise returns an empty list.
另請參閱 canConvert () 和 convert ().
返迴變體如 QTime 若變體擁有 userType () QMetaType::QTime , QMetaType::QDateTime ,或 QMetaType::QString ; otherwise returns an invalid time.
If the type() is QMetaType::QString , an invalid time will be returned if the string cannot be parsed as a Qt::ISODate format time.
另請參閱 canConvert () 和 convert ().
Returns the variant as an unsigned int if the variant has userType () QMetaType::UInt , QMetaType::Bool , QMetaType::QByteArray , QMetaType::QChar , QMetaType::Double , QMetaType::Int , QMetaType::LongLong , QMetaType::QString ,或 QMetaType::ULongLong ;否則返迴 0。
若
ok
非 null:
*
ok
is set to true if the value could be converted to an unsigned int; otherwise
*
ok
被設為 false。
警告: If the value is convertible to a 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 ().
另請參閱 canConvert () 和 convert ().
Returns the variant as an unsigned long long int if the variant has type() QMetaType::ULongLong , QMetaType::Bool , QMetaType::QByteArray , QMetaType::QChar , QMetaType::Double , QMetaType::Int , QMetaType::LongLong , QMetaType::QString ,或 QMetaType::UInt ;否則返迴 0。
若
ok
非 null:
*
ok
is set to true if the value could be converted to an int; otherwise
*
ok
被設為 false。
另請參閱 canConvert () 和 convert ().
返迴變體如 QUrl 若變體擁有 userType () QMetaType::QUrl ;否則返迴無效 QUrl .
另請參閱 canConvert () 和 convert ().
返迴變體如 QUuid if the variant has type() QMetaType::QUuid , QMetaType::QByteArray or QMetaType::QString ;否則返迴默認構造的 QUuid .
另請參閱 canConvert () 和 convert ().
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.
返迴的存儲值被轉換為模闆類型
T
。調用
canConvert
() 以找齣是否可以轉換類型。若無法轉換值,
默認構造值
將被返迴。
若類型
T
支持通過
QVariant
,此函數的行為準確如
toString
(),
toInt
() 等。
範例:
QVariant v; MyCustomStruct c; if (v.canConvert<MyCustomStruct>()) c = v.value<MyCustomStruct>(); v = 7; int i = v.value<int>(); // same as v.toInt() QString s = v.value<QString>(); // same as v.toString(), s is now "7" MyCustomStruct c2 = v.value<MyCustomStruct>(); // conversion failed, c2 is empty
若
QVariant
contains a pointer to a type derived from
QObject
then
T
may be any
QObject
type. If the pointer stored in the
QVariant
可以是
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
宏。
若
QVariant
contains a sequential container and
T
is
QVariantList
, the elements of the container will be converted into
QVariant
s and returned as a
QVariantList
.
QList<int> intList = {7, 11, 42}; QVariant variant = QVariant::fromValue(intList); if (variant.canConvert<QVariantList>()) { QSequentialIterable iterable = variant.value<QSequentialIterable>(); // Can use foreach: foreach (const QVariant &v, iterable) { qDebug() << v; } // Can use C++11 range-for: for (const QVariant &v : iterable) { qDebug() << v; } // Can use iterators: QSequentialIterable::const_iterator it = iterable.begin(); const QSequentialIterable::const_iterator end = iterable.end(); for ( ; it != end; ++it) { qDebug() << *it; } }
另請參閱 setValue (), fromValue (), canConvert (),和 Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE ().
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
。調用
canConvert
() to find out whether a type can be converted. If the value can neither be viewed nor converted, a
默認構造值
將被返迴。
另請參閱 canView () 和 Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE ().
[noexcept]
QVariant
&QVariant::
operator=
(
QVariant
&&
other
)
移動賦值 other 到此 QVariant 實例。
Assigns the value of the variant variant to this variant.
[noexcept, since 6.6]
template <typename T>
T
*
get_if
(
QVariant
*
v
)
[noexcept, since 6.6]
template <typename T> const
T
*
get_if
(const
QVariant
*
v
)
若
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
null
v
with matching type
T
,
v
will not be null after the call.
提供這些函數是為兼容
std::variant
.
該函數在 Qt 6.6 引入。
另請參閱 data ().
[since 6.6]
template <typename T>
T
&
get
(
QVariant
&
v
)
[since 6.6]
template <typename T>
T
&&
get
(
QVariant
&&
v
)
[since 6.6]
template <typename T> const
T
&
get
(const
QVariant
&
v
)
[since 6.6]
template <typename T> const
T
&&
get
(const
QVariant
&&
v
)
若
v
contains an object of type
T
, returns a reference to the contained object, otherwise the call has undefined behavior.
The overloads taking a mutable
v
detach
v
: When called on a
null
v
with matching type
T
,
v
will not be null after the call.
提供這些函數是為兼容
std::variant
.
該函數在 Qt 6.6 引入。
[alias]
QVariantHash
同義詞 QHash < QString , QVariant >.
[alias]
QVariantList
[alias]
QVariantMap
同義詞 QMap < QString , QVariant >.
返迴給定
value
被轉換成模闆類型
T
.
此函數相當於 QVariant::value ().
另請參閱 QVariant::value ().
[since 6.7]
template <typename T>
T
qvariant_cast
(
QVariant
&&
value
)
這是重載函數。
返迴給定
value
被轉換成模闆類型
T
.
該函數在 Qt 6.7 引入。
[noexcept]
bool
operator!=
(const
QVariant
&
lhs
, const
QVariant
&
rhs
)
返迴
false
if
lhs
and
rhs
相等;否則返迴
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:
Writes a variant p 到流 s .
另請參閱 QDataStream 運算符格式 .
[noexcept]
bool
operator==
(const
QVariant
&
lhs
, const
QVariant
&
rhs
)
返迴
true
if
lhs
and
rhs
相等;否則返迴
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:
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.
Reads a variant p 從流 s .
注意: If the stream contains types that aren't the built-in ones (see QMetaType::Type ), those types must be registered using qRegisterMetaType () 或 QMetaType::registerType () before the variant can be properly loaded. If an unregistered type is found, QVariant will set the corrupt flag in the stream, stop processing and print a warning. For example, for QList <int> it would print the following:
QVariant::load: unknown user type with name QList <int>
另請參閱 QDataStream 運算符格式 .