1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
5 \page custom-types.html
6 \title Creating Custom Qt Types
7 \brief How to create and register new types with Qt.
9 \ingroup best-practices
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.
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
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.
27 \section1 Creating a Custom Type
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:
33 \li a public default constructor,
34 \li a public copy constructor, and
35 \li a public destructor.
38 The following \c Message class definition includes these members:
40 \snippet tools/customtype/message.h custom type definition
42 The class also provides a constructor for normal use and two public member functions
43 that are used to obtain the private data.
45 \section1 Declaring the Type with QMetaType
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.
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:
56 \snippet tools/customtype/message.h custom type meta-type declaration
58 This now makes it possible for \c Message values to be stored in QVariant objects
59 and retrieved later. See the \l{Custom Type Example} for code that demonstrates
62 The Q_DECLARE_METATYPE() macro also makes it possible for these values to be used as
63 arguments to signals, but \e{only in direct signal-slot connections}.
64 To make the custom type generally usable with the signals and slots mechanism, we
65 need to perform some extra work.
67 \section1 Creating and Destroying Custom Objects
69 Although the declaration in the previous section makes the type available for use
70 in direct signal-slot connections, it cannot be used for queued signal-slot
71 connections, such as those that are made between objects in different threads.
72 This is because the meta-object system does not know how to handle creation and
73 destruction of objects of the custom type at run-time.
75 To enable creation of objects at run-time, call the qRegisterMetaType() template
76 function to register it with the meta-object system. This also makes the type
77 available for queued signal-slot communication as long as you call it before you
78 make the first connection that uses the type.
80 The \l{Queued Custom Type Example} declares a \c Block class which is registered
81 in the \c{main.cpp} file:
83 \snippet threads/queuedcustomtype/main.cpp main start
85 \snippet threads/queuedcustomtype/main.cpp register meta-type for queued communications
87 \snippet threads/queuedcustomtype/main.cpp main finish
89 This type is later used in a signal-slot connection in the \c{window.cpp} file:
91 \snippet threads/queuedcustomtype/window.cpp Window constructor start
93 \snippet threads/queuedcustomtype/window.cpp connecting signal with custom type
95 \snippet threads/queuedcustomtype/window.cpp Window constructor finish
97 If a type is used in a queued connection without being registered, a warning will be
98 printed at the console; for example:
101 QObject::connect: Cannot queue arguments of type 'Block'
102 (Make sure 'Block' is registered using qRegisterMetaType().)
105 \section1 Making the Type Printable
107 It is often quite useful to make a custom type printable for debugging purposes,
108 as in the following code:
110 \snippet tools/customtype/main.cpp printing a custom type
112 This is achieved by creating a streaming operator for the type, which is often
113 defined in the header file for that type:
115 \snippet tools/customtype/message.h custom type streaming operator
117 The implementation for the \c Message type in the \l{Custom Type Example}
118 goes to some effort to make the printable representation as readable as
121 \snippet tools/customtype/message.cpp custom type streaming operator
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.
129 \section1 Further Reading
131 The Q_DECLARE_METATYPE() macro and qRegisterMetaType() function documentation
132 contain more detailed information about their uses and limitations.
134 The \l{Custom Type Example}{Custom Type} and \l{Queued Custom Type Example}
135 {Queued Custom Type} examples show how to implement a custom type with the
136 features outlined in this document.
138 The \l{Debugging Techniques} document provides an overview of the debugging
139 mechanisms discussed above.