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