Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
6
#
include
<
QDataStream
>
7
#
include
<
QVariant
>
8
#
include
<
QColor
>
9
#
include
<
QPalette
>
10
#
include
<
QMetaSequence
>
11
#
include
<
QMetaAssociation
>
12
13
QString
tr
(
const
char
*s)
14
{
15
return
""
;
16
}
17
18
struct
MyCustomStruct
{};
19
20
QPalette
palette
() {
return
QPalette(); }
21
22
23
QVariant
examples
()
24
{
25
{
26
//! [0]
27
QDataStream out(
"..."
);
28
QVariant v(123);
// The variant now contains an int
29
int
x = v.toInt();
// x = 123
30
out << v;
// Writes a type tag and an int to out
31
v = QVariant(tr(
"hello"
));
// The variant now contains a QString
32
int
y = v.toInt();
// y = 0 since v cannot be converted to an int
33
QString s = v.toString();
// s = tr("hello") (see QObject::tr())
34
out << v;
// Writes a type tag and a QString to out
35
//...
36
QDataStream in(
"..."
);
// (opening the previously written stream)
37
in >> v;
// Reads an Int variant
38
int
z = v.toInt();
// z = 123
39
qDebug(
"Type is %s"
,
// prints "Type is int"
40
v.typeName());
41
v = v.toInt() + 100;
// The variant now holds the value 223
42
v = QVariant(QStringList());
// The variant now holds a QStringList
43
//! [0]
44
}
45
46
{
47
//! [1]
48
QVariant x;
// x.isNull() == true
49
QVariant y = QVariant::fromValue(
nullptr
);
// y.isNull() == true
50
//! [1]
51
}
52
53
{
54
//! [2]
55
QVariant variant;
56
//...
57
QColor color = variant.value<QColor>();
58
//! [2]
59
}
60
61
{
62
//! [3]
63
QColor color = palette().window().color();
64
QVariant variant = color;
65
//! [3]
66
}
67
68
{
69
//! [4]
70
QVariant v;
71
72
v.setValue(5);
73
int
i = v.toInt();
// i is now 5
74
QString s = v.toString();
// s is now "5"
75
76
MyCustomStruct
c;
77
v.setValue(c);
78
79
//...
80
81
MyCustomStruct
c2 = v.value<
MyCustomStruct
>();
82
//! [4]
83
}
84
85
{
86
//! [5]
87
QVariant v;
88
89
MyCustomStruct
c;
90
if
(v.canConvert<
MyCustomStruct
>())
91
c = v.value<
MyCustomStruct
>();
92
93
v = 7;
94
int
i = v.value<
int
>();
// same as v.toInt()
95
QString s = v.value<QString>();
// same as v.toString(), s is now "7"
96
MyCustomStruct
c2 = v.value<
MyCustomStruct
>();
// conversion failed, c2 is empty
97
//! [5]
98
}
99
100
{
101
//! [6]
102
QVariant v = 42;
103
104
v.canConvert<
int
>();
// returns true
105
v.canConvert<QString>();
// returns true
106
107
MyCustomStruct
s;
108
v.setValue(s);
109
110
v.canConvert<
int
>();
// returns false
111
v.canConvert<
MyCustomStruct
>();
// returns true
112
//! [6]
113
}
114
115
{
116
//! [7]
117
MyCustomStruct
s;
118
return
QVariant::fromValue(s);
119
//! [7]
120
}
121
122
{
123
//! [9]
124
QList<
int
> intList = {7, 11, 42};
125
126
QVariant variant = QVariant::fromValue(intList);
127
if
(variant.canConvert<QVariantList>()) {
128
QMetaSequence::Iterable iterable = variant.value<QMetaSequence::Iterable>();
129
// Can use C++11 range-for:
130
for
(
const
QVariant &v : iterable) {
131
qDebug() << v;
132
}
133
// Can use iterators:
134
QMetaSequence::Iterable::const_iterator it = iterable.begin();
135
const
QMetaSequence::Iterable::const_iterator end = iterable.end();
136
for
( ; it != end; ++it) {
137
qDebug() << *it;
138
}
139
}
140
//! [9]
141
}
142
143
{
144
//! [10]
145
QHash<
int
, QString> mapping;
146
mapping.insert(7,
"Seven"
);
147
mapping.insert(11,
"Eleven"
);
148
mapping.insert(42,
"Forty-two"
);
149
150
QVariant variant = QVariant::fromValue(mapping);
151
if
(variant.canConvert<QVariantHash>()) {
152
QMetaAssociation::Iterable iterable = variant.value<QMetaAssociation::Iterable>();
153
// Can use C++11 range-for over the values:
154
for
(
const
QVariant &v : iterable) {
155
qDebug() << v;
156
}
157
// Can use iterators:
158
QMetaAssociation::Iterable::const_iterator it = iterable.begin();
159
const
QMetaAssociation::Iterable::const_iterator end = iterable.end();
160
for
( ; it != end; ++it) {
161
qDebug() << *it;
// The current value
162
qDebug() << it.key();
163
qDebug() << it.value();
164
}
165
}
166
//! [10]
167
}
168
return
QVariant();
169
}
examples
bool examples()
[3]
Definition
src_corelib_global_qglobal.cpp:56
tr
QString tr(const char *)
Definition
src_corelib_global_qglobal_widgets.cpp:67
palette
QPalette palette()
Definition
src_corelib_kernel_qvariant.cpp:20
MyCustomStruct
Definition
src_corelib_kernel_qvariant.cpp:18
qtbase
src
corelib
doc
snippets
code
src_corelib_kernel_qvariant.cpp
Generated on
for Qt by
1.18.0