~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/corelib/doc/src/custom-types.qdoc

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the documentation of the Qt Toolkit.
 
7
**
 
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.
 
16
**
 
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.
 
24
** $QT_END_LICENSE$
 
25
**
 
26
****************************************************************************/
 
27
 
 
28
/*!
 
29
    \page custom-types.html
 
30
    \title Creating Custom Qt Types
 
31
    \brief How to create and register new types with Qt.
 
32
 
 
33
    \ingroup best-practices
 
34
 
 
35
    \tableofcontents
 
36
 
 
37
    \section1 Overview
 
38
 
 
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.
 
42
 
 
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
 
45
    communication.
 
46
 
 
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.
 
50
 
 
51
    \section1 Creating a Custom Type
 
52
 
 
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:
 
55
 
 
56
    \list
 
57
    \li a public default constructor,
 
58
    \li a public copy constructor, and
 
59
    \li a public destructor.
 
60
    \endlist
 
61
 
 
62
    The following \c Message class definition includes these members:
 
63
 
 
64
    \snippet customtype/message.h custom type definition
 
65
 
 
66
    The class also provides a constructor for normal use and two public member functions
 
67
    that are used to obtain the private data.
 
68
 
 
69
    \section1 Declaring the Type with QMetaType
 
70
 
 
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.
 
75
 
 
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:
 
79
 
 
80
    \snippet customtype/message.h custom type meta-type declaration
 
81
 
 
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
 
84
    this.
 
85
 
 
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.
 
90
 
 
91
    \section1 Creating and Destroying Custom Objects
 
92
 
 
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.
 
98
 
 
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.
 
103
 
 
104
    The \l{Queued Custom Type Example} declares a \c Block class which is registered
 
105
    in the \c{main.cpp} file:
 
106
 
 
107
    \snippet queuedcustomtype/main.cpp main start
 
108
    \dots
 
109
    \snippet queuedcustomtype/main.cpp register meta-type for queued communications
 
110
    \dots
 
111
    \snippet queuedcustomtype/main.cpp main finish
 
112
 
 
113
    This type is later used in a signal-slot connection in the \c{window.cpp} file:
 
114
 
 
115
    \snippet queuedcustomtype/window.cpp Window constructor start
 
116
    \dots
 
117
    \snippet queuedcustomtype/window.cpp connecting signal with custom type
 
118
    \dots
 
119
    \snippet queuedcustomtype/window.cpp Window constructor finish
 
120
 
 
121
    If a type is used in a queued connection without being registered, a warning will be
 
122
    printed at the console; for example:
 
123
 
 
124
    \code
 
125
    QObject::connect: Cannot queue arguments of type 'Block'
 
126
    (Make sure 'Block' is registered using qRegisterMetaType().)
 
127
    \endcode
 
128
 
 
129
    \section1 Making the Type Printable
 
130
 
 
131
    It is often quite useful to make a custom type printable for debugging purposes,
 
132
    as in the following code:
 
133
 
 
134
    \snippet customtype/main.cpp printing a custom type
 
135
 
 
136
    This is achieved by creating a streaming operator for the type, which is often
 
137
    defined in the header file for that type:
 
138
 
 
139
    \snippet customtype/message.h custom type streaming operator
 
140
 
 
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
 
143
    possible:
 
144
 
 
145
    \snippet customtype/message.cpp custom type streaming operator
 
146
 
 
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.
 
152
 
 
153
    \section1 Further Reading
 
154
 
 
155
    The Q_DECLARE_METATYPE() macro and qRegisterMetaType() function documentation
 
156
    contain more detailed information about their uses and limitations.
 
157
 
 
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.
 
162
 
 
163
    The \l{Debugging Techniques} document provides an overview of the debugging
 
164
    mechanisms discussed above.
 
165
*/