QtBase  v6.3.1
src_corelib_kernel_qmetatype.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 
52 struct MyStruct
53 {
54  int i;
55  ...
56 };
57 
60 
61 
62 namespace MyNamespace
64 {
65  ...
66 }
67 
68 Q_DECLARE_METATYPE(MyNamespace::MyStruct)
70 
71 
72 MyStruct s;
75 var.setValue(s); // copy s into the variant
76 
77 ...
78 
79 // retrieve the value
80 MyStruct s2 = var.value<MyStruct>();
82 
83 
85 int id = QMetaType::type("MyClass");
87  void *myClassPtr = QMetaType::create(id);
88  ...
89  QMetaType::destroy(id, myClassPtr);
90  myClassPtr = 0;
91 }
93 
94 
98 
99 
104 
105 
107 int id = qRegisterMetaType<MyStruct>();
109 
110 
112 int id = qMetaTypeId<QString>(); // id is now QMetaType::QString
113 id = qMetaTypeId<MyStruct>(); // compile error if MyStruct not declared
115 
118 qRegisterMetaType<CustomString>("CustomString");
120 
122 
123 #include <deque>
124 
126 
128 {
129  std::deque<QFile*> container;
130  QVariant var = QVariant::fromValue(container);
131  // ...
132 }
133 
135 
137 
138 #include <unordered_list>
139 
141 
142 void someFunc()
143 {
144  std::unordered_map<int, bool> container;
145  QVariant var = QVariant::fromValue(container);
146  // ...
147 }
148 
150 
154 // ...
156  QObject *sp = var.value<QObject*>();
157  qDebug() << sp->metaObject()->className(); // Prints 'QFile'.
158 }
160 
162 
163 #include <memory>
164 
165 Q_DECLARE_SMART_POINTER_METATYPE(std::shared_ptr)
166 
167 void someFunc()
168 {
169  auto smart_ptr = std::make_shared<QFile>();
170  QVariant var = QVariant::fromValue(smart_ptr);
171  // ...
172  if (var.canConvert<QObject*>()) {
173  QObject *sp = var.value<QObject*>();
174  qDebug() << sp->metaObject()->className(); // Prints 'QFile'.
175  }
176 }
177 
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:66
operator>>(QDataStream &ds, qfloat16 &f)
Definition: qfloat16.cpp:344
operator<<(QDataStream &ds, qfloat16 f)
Definition: qfloat16.cpp:327
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:94
void destroy(void *data) const
Definition: qmetatype.cpp:639
@ UnknownType
Definition: qmetatype.h:346
void * create(const void *copy=nullptr) const
Definition: qmetatype.cpp:616
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
The QString class provides a Unicode character string.
Definition: qstring.h:388
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
T value() const
Definition: qvariant.h:378
bool canConvert(QMetaType targetType) const
Definition: qvariant.h:246
void setValue(T &&avalue)
Definition: qvariant.h:355
void
Definition: png.h:1080
#define qDebug
[1]
Definition: qlogging.h:177
#define Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(SINGLE_ARG_TEMPLATE)
Definition: qmetatype.h:1586
#define Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE(TEMPLATENAME)
Definition: qmetatype.h:1610
#define Q_DECLARE_METATYPE(TYPE)
Definition: qmetatype.h:1417
#define Q_DECLARE_SMART_POINTER_METATYPE(SMART_POINTER)
Definition: qmetatype.h:1539
GLenum type
Definition: qopengl.h:270
GLuint in
Definition: qopenglext.h:8870
GLdouble s
[6]
Definition: qopenglext.h:235
#define sp
QTextStream out(stdout)
[7]
qRegisterMetaType< CustomString >("CustomString")
MyStruct s
[1]
QPointer< QFile > fp(new QFile)
[11]
void someFunc()
[9]
qRegisterMetaType< MyClass >("MyClass")
[3]
QString CustomString
[8]