Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
src_corelib_kernel_qvariant.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses
5
8QVariant v(123); // The variant now contains an int
9int x = v.toInt(); // x = 123
10out << v; // Writes a type tag and an int to out
11v = QVariant(tr("hello")); // The variant now contains a QString
12int y = v.toInt(); // y = 0 since v cannot be converted to an int
13QString s = v.toString(); // s = tr("hello") (see QObject::tr())
14out << v; // Writes a type tag and a QString to out
15...
16QDataStream in(...); // (opening the previously written stream)
17in >> v; // Reads an Int variant
18int z = v.toInt(); // z = 123
19qDebug("Type is %s", // prints "Type is int"
20 v.typeName());
21v = v.toInt() + 100; // The variant now holds the value 223
22v = QVariant(QStringList()); // The variant now holds a QStringList
24
26QVariant x; // x.isNull() == true
27QVariant y = QVariant::fromValue(nullptr); // y.isNull() == true
29
30
33...
36
37
39QColor color = palette().background().color();
42
43
46
48int i = v.toInt(); // i is now 5
49QString s = v.toString(); // s is now "5"
50
51MyCustomStruct c;
52v.setValue(c);
53
54...
55
56MyCustomStruct c2 = v.value<MyCustomStruct>();
58
59
62
63MyCustomStruct c;
64if (v.canConvert<MyCustomStruct>())
65 c = v.value<MyCustomStruct>();
66
67v = 7;
68int i = v.value<int>(); // same as v.toInt()
69QString s = v.value<QString>(); // same as v.toString(), s is now "7"
70MyCustomStruct c2 = v.value<MyCustomStruct>(); // conversion failed, c2 is empty
72
73
75QVariant v = 42;
76
77v.canConvert<int>(); // returns true
78v.canConvert<QString>(); // returns true
79
80MyCustomStruct s;
81v.setValue(s);
82
83v.canConvert<int>(); // returns false
84v.canConvert<MyCustomStruct>(); // returns true
86
87
89MyCustomStruct s;
92
93
95
96QList<int> intList = {7, 11, 42};
97
101 // Can use foreach:
102 foreach (const QVariant &v, iterable) {
103 qDebug() << v;
104 }
105 // Can use C++11 range-for:
106 for (const QVariant &v : iterable) {
107 qDebug() << v;
108 }
109 // Can use iterators:
111 const QSequentialIterable::const_iterator end = iterable.end();
112 for ( ; it != end; ++it) {
113 qDebug() << *it;
114 }
115}
116
118
120
121QHash<int, QString> mapping;
122mapping.insert(7, "Seven");
123mapping.insert(11, "Eleven");
124mapping.insert(42, "Forty-two");
125
129 // Can use foreach over the values:
130 foreach (const QVariant &v, iterable) {
131 qDebug() << v;
132 }
133 // Can use C++11 range-for over the values:
134 for (const QVariant &v : iterable) {
135 qDebug() << v;
136 }
137 // Can use iterators:
139 const QAssociativeIterable::const_iterator end = iterable.end();
140 for ( ; it != end; ++it) {
141 qDebug() << *it; // The current value
142 qDebug() << it.key();
143 qDebug() << it.value();
144 }
145}
146
The QAssociativeIterable class is an iterable interface for an associative container in a QVariant.
QTaggedIterator< QAssociativeConstIterator, void > const_iterator
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
\inmodule QtCore\reentrant
Definition qdatastream.h:46
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1303
The QSequentialIterable class is an iterable interface for a container in a QVariant.
QTaggedIterator< QSequentialConstIterator, void > const_iterator
iterator begin()
Definition qset.h:136
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qvariant.h:65
T value() const &
Definition qvariant.h:516
void setValue(T &&avalue)
Definition qvariant.h:493
int toInt(bool *ok=nullptr) const
Returns the variant as an int if the variant has userType() \l QMetaType::Int, \l QMetaType::Bool,...
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 >
Definition qvariant.h:536
bool canConvert(QMetaType targetType) const
Definition qvariant.h:345
QSet< QString >::iterator it
QList< QString > QStringList
Constructs a string list that contains the given string, str.
#define qDebug
[1]
Definition qlogging.h:164
GLsizei const GLfloat * v
[13]
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat z
GLint GLint GLint GLint GLint x
[0]
GLuint GLuint end
GLuint color
[2]
GLint y
GLdouble s
[6]
Definition qopenglext.h:235
const GLubyte * c
GLuint in
GLenum GLenum GLenum GLenum mapping
#define tr(X)
QVariant v(123)
[3]
QVariant variant
[1]
QDataStream out(...)
[0]
QList< int > intList
[7]
MyCustomStruct c
QColor color
[2]
QHash< int, QString > mapping
[9]
MyCustomStruct c2