Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
custom-types.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page custom-types.html
6
\title Creating Custom Qt Types
7
\brief How to create and register new types with Qt.
8
9
\ingroup how-to
10
11
\section1 Overview
12
13
When creating user interfaces with Qt, particularly those with specialized controls and
14
features, developers sometimes need to create new data types that can be used alongside
15
or in place of Qt's existing set of value types.
16
17
Standard types such as QSize, QColor and QString can all be stored in QVariant objects,
18
used as the types of properties in QObject-based classes, and emitted in signal-slot
19
communication.
20
21
In this document, we take a custom type and describe how to integrate it into Qt's object
22
model so that it can be stored in the same way as standard Qt types. We then show how to
23
register the custom type to allow it to be used in signals and slots connections.
24
25
\section1 Creating a Custom Type
26
27
Before we begin, we need to ensure that the custom type we are creating meets all the
28
requirements imposed by QMetaType. In other words, it must provide:
29
30
\list
31
\li a public default constructor,
32
\li a public copy constructor, and
33
\li a public destructor.
34
\endlist
35
36
The following \c Message class definition includes these members:
37
38
\snippet customtype/customtypeexample.cpp custom type definition
39
40
The class also provides a constructor for normal use and two public member functions
41
that are used to obtain the private data.
42
43
\section1 Declaring the Type with QMetaType
44
45
The \c Message class only needs a suitable implementation in order to be usable.
46
However, Qt's type system will not be able to understand how to store, retrieve
47
and serialize instances of this class without some assistance. For example, we
48
will be unable to store \c Message values in QVariant.
49
50
The class in Qt responsible for custom types is QMetaType. To make the type known
51
to this class, we invoke the Q_DECLARE_METATYPE() macro on the class in the header
52
file where it is defined:
53
54
\snippet customtype/customtypeexample.cpp custom type meta-type declaration
55
56
This now makes it possible for \c Message values to be stored in QVariant objects
57
and retrieved later:
58
59
\snippet customtype/customtypeexample.cpp storing a custom value
60
\dots
61
\snippet customtype/customtypeexample.cpp retrieving a custom value
62
63
The Q_DECLARE_METATYPE() macro also makes it possible for these values to be used as
64
arguments to signals, but \e{only in direct signal-slot connections}.
65
To make the custom type generally usable with the signals and slots mechanism, we
66
need to perform some extra work.
67
68
\section1 Creating and Destroying Custom Objects
69
70
Although the declaration in the previous section makes the type available for use
71
in direct signal-slot connections, it cannot be used for queued signal-slot
72
connections, such as those that are made between objects in different threads.
73
This is because the meta-object system does not know how to handle creation and
74
destruction of objects of the custom type at run-time.
75
76
To enable creation of objects at run-time, call the qRegisterMetaType() template
77
function to register it with the meta-object system. This also makes the type
78
available for queued signal-slot communication as long as you call it before you
79
make the first connection that uses the type.
80
81
The \l{Queued Custom Type} example declares a \c Block class which is registered
82
in the \c{main.cpp} file:
83
84
\snippet threads/queuedcustomtype/main.cpp main start
85
\dots
86
\snippet threads/queuedcustomtype/main.cpp register meta-type for queued communications
87
\dots
88
\snippet threads/queuedcustomtype/main.cpp main finish
89
90
This type is later used in a signal-slot connection in the \c{window.cpp} file:
91
92
\snippet threads/queuedcustomtype/window.cpp Window constructor start
93
\dots
94
\snippet threads/queuedcustomtype/window.cpp connecting signal with custom type
95
\dots
96
\snippet threads/queuedcustomtype/window.cpp Window constructor finish
97
98
If a type is used in a queued connection without being registered, a warning will be
99
printed at the console; for example:
100
101
\code
102
QObject::connect: Cannot queue arguments of type 'Block'
103
(Make sure 'Block' is registered using qRegisterMetaType().)
104
\endcode
105
106
\section1 Making the Type Printable
107
108
It is often quite useful to make a custom type printable for debugging purposes,
109
as in the following code:
110
111
\snippet customtype/customtypeexample.cpp printing a custom type
112
113
This is achieved by creating a streaming operator for the type, which is often
114
defined in the header file for that type:
115
116
\snippet customtype/customtypeexample.cpp custom type streaming operator declaration
117
118
The implementation for the \c Message type here goes to some effort to make the
119
printable representation as readable as possible:
120
121
\snippet customtype/customtypeexample.cpp custom type streaming operator
122
123
The output sent to the debug stream can, of course, be made as simple or as
124
complicated as you like. Note that the value returned by this function is
125
the QDebug object itself, though this is often obtained by calling the
126
\l{QDebug::}{maybeSpace()} member function of QDebug that pads out the stream with space
127
characters to make it more readable.
128
129
\section1 Further Reading
130
131
The Q_DECLARE_METATYPE() macro and qRegisterMetaType() function documentation
132
contain more detailed information about their uses and limitations.
133
134
The \l{Queued Custom Type} example shows how to implement a custom type with
135
the features outlined in this document.
136
137
The \l{Debugging Techniques} document provides an overview of the debugging
138
mechanisms discussed above.
139
*/
qtbase
src
corelib
doc
src
custom-types.qdoc
Generated on
for Qt by
1.14.0