Qt 6.x
The Qt SDK
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
6QVariant v(123); // The variant now contains an int
7int x = v.toInt(); // x = 123
8out << v; // Writes a type tag and an int to out
9v = QVariant(tr("hello")); // The variant now contains a QString
10int y = v.toInt(); // y = 0 since v cannot be converted to an int
11QString s = v.toString(); // s = tr("hello") (see QObject::tr())
12out << v; // Writes a type tag and a QString to out
13...
14QDataStream in(...); // (opening the previously written stream)
15in >> v; // Reads an Int variant
16int z = v.toInt(); // z = 123
17qDebug("Type is %s", // prints "Type is int"
18 v.typeName());
19v = v.toInt() + 100; // The variant now holds the value 223
20v = QVariant(QStringList()); // The variant now holds a QStringList
22
24QVariant x; // x.isNull() == true
25QVariant y = QVariant::fromValue(nullptr); // y.isNull() == true
27
28
31...
34
35
37QColor color = palette().background().color();
40
41
44
46int i = v.toInt(); // i is now 5
47QString s = v.toString(); // s is now "5"
48
49MyCustomStruct c;
50v.setValue(c);
51
52...
53
54MyCustomStruct c2 = v.value<MyCustomStruct>();
56
57
60
61MyCustomStruct c;
62if (v.canConvert<MyCustomStruct>())
63 c = v.value<MyCustomStruct>();
64
65v = 7;
66int i = v.value<int>(); // same as v.toInt()
67QString s = v.value<QString>(); // same as v.toString(), s is now "7"
68MyCustomStruct c2 = v.value<MyCustomStruct>(); // conversion failed, c2 is empty
70
71
73QVariant v = 42;
74
75v.canConvert<int>(); // returns true
76v.canConvert<QString>(); // returns true
77
78MyCustomStruct s;
79v.setValue(s);
80
81v.canConvert<int>(); // returns false
82v.canConvert<MyCustomStruct>(); // returns true
84
85
87MyCustomStruct s;
90
91
93
94QList<int> intList = {7, 11, 42};
95
99 // Can use foreach:
100 foreach (const QVariant &v, iterable) {
101 qDebug() << v;
102 }
103 // Can use C++11 range-for:
104 for (const QVariant &v : iterable) {
105 qDebug() << v;
106 }
107 // Can use iterators:
110 for ( ; it != end; ++it) {
111 qDebug() << *it;
112 }
113}
114
116
118
120mapping.insert(7, "Seven");
121mapping.insert(11, "Eleven");
122mapping.insert(42, "Forty-two");
123
127 // Can use foreach over the values:
128 foreach (const QVariant &v, iterable) {
129 qDebug() << v;
130 }
131 // Can use C++11 range-for over the values:
132 for (const QVariant &v : iterable) {
133 qDebug() << v;
134 }
135 // Can use iterators:
138 for ( ; it != end; ++it) {
139 qDebug() << *it; // The current value
140 qDebug() << it.key();
141 qDebug() << it.value();
142 }
143}
144
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:31
\inmodule QtCore\reentrant
Definition qdatastream.h:30
\inmodule QtCore
Definition qhash.h:818
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1283
Definition qlist.h:74
The QSequentialIterable class is an iterable interface for a container in a QVariant.
const_iterator end() const
const_iterator begin() const
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QTaggedIterator is a template class that wraps an iterator and exposes standard iterator traits.
Definition qiterable.h:68
\inmodule QtCore
Definition qvariant.h:64
T value() const &
Definition qvariant.h:511
int toInt(bool *ok=nullptr) const
Returns the variant as an int if the variant has userType() \l QMetaType::Int, \l QMetaType::Bool,...
bool canConvert(QMetaType targetType) const
Definition qvariant.h:342
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:531
void setValue(T &&avalue)
Stores a copy of value.
Definition qvariant.h:488
QSet< QString >::iterator it
#define qDebug
[1]
Definition qlogging.h:160
GLsizei const GLfloat * v
[13]
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat z
GLint GLint GLint GLint GLint x
[0]
GLuint GLuint end
GLint y
const GLubyte * c
GLuint in
GLdouble s
[6]
Definition qopenglext.h:235
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