QtBase  v6.3.1
src_corelib_kernel_qvariant.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
53 QVariant v(123); // The variant now contains an int
54 int x = v.toInt(); // x = 123
55 out << v; // Writes a type tag and an int to out
56 v = QVariant(tr("hello")); // The variant now contains a QString
57 int y = v.toInt(); // y = 0 since v cannot be converted to an int
58 QString s = v.toString(); // s = tr("hello") (see QObject::tr())
59 out << v; // Writes a type tag and a QString to out
60 ...
61 QDataStream in(...); // (opening the previously written stream)
62 in >> v; // Reads an Int variant
63 int z = v.toInt(); // z = 123
64 qDebug("Type is %s", // prints "Type is int"
65  v.typeName());
66 v = v.toInt() + 100; // The variant now holds the value 223
67 v = QVariant(QStringList()); // The variant now holds a QStringList
69 
71 QVariant x; // x.isNull() == true
72 QVariant y = QVariant::fromValue(nullptr); // y.isNull() == true
74 
75 
78 ...
79 QColor color = variant.value<QColor>();
81 
82 
84 QColor color = palette().background().color();
87 
88 
90 QVariant v;
91 
93 int i = v.toInt(); // i is now 5
94 QString s = v.toString(); // s is now "5"
95 
96 MyCustomStruct c;
97 v.setValue(c);
98 
99 ...
100 
101 MyCustomStruct c2 = v.value<MyCustomStruct>();
103 
104 
106 QVariant v;
107 
108 MyCustomStruct c;
109 if (v.canConvert<MyCustomStruct>())
110  c = v.value<MyCustomStruct>();
111 
112 v = 7;
113 int i = v.value<int>(); // same as v.toInt()
114 QString s = v.value<QString>(); // same as v.toString(), s is now "7"
115 MyCustomStruct c2 = v.value<MyCustomStruct>(); // conversion failed, c2 is empty
117 
118 
120 QVariant v = 42;
121 
122 v.canConvert<int>(); // returns true
123 v.canConvert<QString>(); // returns true
124 
125 MyCustomStruct s;
126 v.setValue(s);
127 
128 v.canConvert<int>(); // returns false
129 v.canConvert<MyCustomStruct>(); // returns true
131 
132 
134 MyCustomStruct s;
135 return QVariant::fromValue(s);
137 
138 
140 QObject *object = getObjectFromSomewhere();
143 
145 
146 QList<int> intList = {7, 11, 42};
147 
151  // Can use foreach:
152  foreach (const QVariant &v, iterable) {
153  qDebug() << v;
154  }
155  // Can use C++11 range-for:
156  for (const QVariant &v : iterable) {
157  qDebug() << v;
158  }
159  // Can use iterators:
161  const QSequentialIterable::const_iterator end = iterable.end();
162  for ( ; it != end; ++it) {
163  qDebug() << *it;
164  }
165 }
166 
168 
170 
172 mapping.insert(7, "Seven");
173 mapping.insert(11, "Eleven");
174 mapping.insert(42, "Forty-two");
175 
179  // Can use foreach over the values:
180  foreach (const QVariant &v, iterable) {
181  qDebug() << v;
182  }
183  // Can use C++11 range-for over the values:
184  for (const QVariant &v : iterable) {
185  qDebug() << v;
186  }
187  // Can use iterators:
189  const QAssociativeIterable::const_iterator end = iterable.end();
190  for ( ; it != end; ++it) {
191  qDebug() << *it; // The current value
192  qDebug() << it.key();
193  qDebug() << it.value();
194  }
195 }
196 
The QAssociativeIterable class is an iterable interface for an associative container in a QVariant.
const_iterator begin() const
const_iterator end() const
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:66
iterator insert(const Key &key, const T &value)
Definition: qhash.h:1228
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
The QSequentialIterable class is an iterable interface for a container in a QVariant.
const_iterator end() const
const_iterator begin() const
The QString class provides a Unicode character string.
Definition: qstring.h:388
QTaggedIterator is a template class that wraps an iterator and exposes standard iterator traits.
Definition: qiterable.h:104
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:95
static auto fromValue(const T &value) -> std::enable_if_t< std::is_copy_constructible_v< T >, QVariant >
Definition: qvariant.h:391
int toInt(bool *ok=nullptr) const
Definition: qvariant.cpp:1833
T value() const
Definition: qvariant.h:378
bool canConvert(QMetaType targetType) const
Definition: qvariant.h:246
void setValue(T &&avalue)
Definition: qvariant.h:355
palette
QList< QString > QStringList
Definition: qcontainerfwd.h:64
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 GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint y
const GLubyte * c
Definition: qopenglext.h:12701
GLuint in
Definition: qopenglext.h:8870
GLdouble s
[6]
Definition: qopenglext.h:235
GLenum GLenum GLenum GLenum mapping
Definition: qopenglext.h:10816
#define tr(X)
QVariant v(123)
[3]
QVariant variant
[1]
QDataStream out(...)
[0]
QList< int > intList
[8]
MyCustomStruct c
QString s
[6]
qDebug("Type is %s", v.typeName())
QColor color
[2]
QHash< int, QString > mapping
[9]
MyCustomStruct c2
QStringList::Iterator it