1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the documentation of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:FDL$
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 Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Free Documentation License Usage
18
** Alternatively, this file may be used under the terms of the GNU Free
19
** Documentation License version 1.3 as published by the Free Software
20
** Foundation and appearing in the file included in the packaging of
21
** this file. Please review the following information to ensure
22
** the GNU Free Documentation License version 1.3 requirements
23
** will be met: http://www.gnu.org/copyleft/fdl.html.
26
****************************************************************************/
29
\page custom-types.html
30
\title Creating Custom Qt Types
31
\brief How to create and register new types with Qt.
33
\ingroup best-practices
39
When creating user interfaces with Qt, particularly those with specialized controls and
40
features, developers sometimes need to create new data types that can be used alongside
41
or in place of Qt's existing set of value types.
43
Standard types such as QSize, QColor and QString can all be stored in QVariant objects,
44
used as the types of properties in QObject-based classes, and emitted in signal-slot
47
In this document, we take a custom type and describe how to integrate it into Qt's object
48
model so that it can be stored in the same way as standard Qt types. We then show how to
49
register the custom type to allow it to be used in signals and slots connections.
51
\section1 Creating a Custom Type
53
Before we begin, we need to ensure that the custom type we are creating meets all the
54
requirements imposed by QMetaType. In other words, it must provide:
57
\li a public default constructor,
58
\li a public copy constructor, and
59
\li a public destructor.
62
The following \c Message class definition includes these members:
64
\snippet customtype/message.h custom type definition
66
The class also provides a constructor for normal use and two public member functions
67
that are used to obtain the private data.
69
\section1 Declaring the Type with QMetaType
71
The \c Message class only needs a suitable implementation in order to be usable.
72
However, Qt's type system will not be able to understand how to store, retrieve
73
and serialize instances of this class without some assistance. For example, we
74
will be unable to store \c Message values in QVariant.
76
The class in Qt responsible for custom types is QMetaType. To make the type known
77
to this class, we invoke the Q_DECLARE_METATYPE() macro on the class in the header
78
file where it is defined:
80
\snippet customtype/message.h custom type meta-type declaration
82
This now makes it possible for \c Message values to be stored in QVariant objects
83
and retrieved later. See the \l{Custom Type Example} for code that demonstrates
86
The Q_DECLARE_METATYPE() macro also makes it possible for these values to be used as
87
arguments to signals, but \e{only in direct signal-slot connections}.
88
To make the custom type generally usable with the signals and slots mechanism, we
89
need to perform some extra work.
91
\section1 Creating and Destroying Custom Objects
93
Although the declaration in the previous section makes the type available for use
94
in direct signal-slot connections, it cannot be used for queued signal-slot
95
connections, such as those that are made between objects in different threads.
96
This is because the meta-object system does not know how to handle creation and
97
destruction of objects of the custom type at run-time.
99
To enable creation of objects at run-time, call the qRegisterMetaType() template
100
function to register it with the meta-object system. This also makes the type
101
available for queued signal-slot communication as long as you call it before you
102
make the first connection that uses the type.
104
The \l{Queued Custom Type Example} declares a \c Block class which is registered
105
in the \c{main.cpp} file:
107
\snippet queuedcustomtype/main.cpp main start
109
\snippet queuedcustomtype/main.cpp register meta-type for queued communications
111
\snippet queuedcustomtype/main.cpp main finish
113
This type is later used in a signal-slot connection in the \c{window.cpp} file:
115
\snippet queuedcustomtype/window.cpp Window constructor start
117
\snippet queuedcustomtype/window.cpp connecting signal with custom type
119
\snippet queuedcustomtype/window.cpp Window constructor finish
121
If a type is used in a queued connection without being registered, a warning will be
122
printed at the console; for example:
125
QObject::connect: Cannot queue arguments of type 'Block'
126
(Make sure 'Block' is registered using qRegisterMetaType().)
129
\section1 Making the Type Printable
131
It is often quite useful to make a custom type printable for debugging purposes,
132
as in the following code:
134
\snippet customtype/main.cpp printing a custom type
136
This is achieved by creating a streaming operator for the type, which is often
137
defined in the header file for that type:
139
\snippet customtype/message.h custom type streaming operator
141
The implementation for the \c Message type in the \l{Custom Type Example}
142
goes to some effort to make the printable representation as readable as
145
\snippet customtype/message.cpp custom type streaming operator
147
The output sent to the debug stream can, of course, be made as simple or as
148
complicated as you like. Note that the value returned by this function is
149
the QDebug object itself, though this is often obtained by calling the
150
maybeSpace() member function of QDebug that pads out the stream with space
151
characters to make it more readable.
153
\section1 Further Reading
155
The Q_DECLARE_METATYPE() macro and qRegisterMetaType() function documentation
156
contain more detailed information about their uses and limitations.
158
The \l{Custom Type Example}{Custom Type},
159
\l{Custom Type Sending Example}{Custom Type Sending}
160
and \l{Queued Custom Type Example}{Queued Custom Type} examples show how to
161
implement a custom type with the features outlined in this document.
163
The \l{Debugging Techniques} document provides an overview of the debugging
164
mechanisms discussed above.