source: trunk/doc/src/examples/customwidgetplugin.qdoc@ 1168

Last change on this file since 1168 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 9.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:FDL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in a
14** written agreement between you and Nokia.
15**
16** GNU Free Documentation License
17** Alternatively, this file may be used under the terms of the GNU Free
18** Documentation License version 1.3 as published by the Free Software
19** Foundation and appearing in the file included in the packaging of this
20** file.
21**
22** If you have questions regarding the use of this file, please contact
23** Nokia at [email protected].
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29 \example designer/customwidgetplugin
30 \title Custom Widget Plugin Example
31
32 The Custom Widget example shows how to create a custom widget plugin for \QD.
33
34 \image customwidgetplugin-example.png
35
36 In this example, the custom widget used is based on the
37 \l{widgets/analogclock}{Analog Clock example}, and does not provide any custom
38 signals or slots.
39
40 \section1 Preparation
41
42 To provide a custom widget that can be used with \QD, we need to supply a
43 self-contained implementation and provide a plugin interface. In this
44 example, we reuse the \l{widgets/analogclock}{Analog Clock example} for
45 convenience.
46
47 Since custom widgets plugins rely on components supplied with \QD, the
48 project file that we use needs to contain information about \QD's
49 library components:
50
51 \snippet examples/designer/customwidgetplugin/customwidgetplugin.pro 2
52 \snippet examples/designer/customwidgetplugin/customwidgetplugin.pro 0
53
54 The \c TEMPLATE variable's value makes \c qmake create the custom
55 widget as a library. Later, we will ensure that the widget will be
56 recognized as a plugin by Qt by using the Q_EXPORT_PLUGIN2() macro
57 to export the relevant widget information.
58
59 The \c CONFIG variable contains two values, \c designer and \c
60 plugin:
61
62 \list
63
64 \o \c designer: Since custom widgets plugins rely on components
65 supplied with \QD, this value ensures that our plugin links
66 against \QD's library (\c libQtDesigner.so).
67
68 \o \c plugin: We also need to ensure that \c qmake considers the
69 custom widget a plugin library.
70
71 \endlist
72
73 When Qt is configured to build in both debug and release modes,
74 \QD will be built in release mode. When this occurs, it is
75 necessary to ensure that plugins are also built in release
76 mode. For that reason we add the \c debug_and_release value to the
77 \c CONFIG variable. Otherwise, if a plugin is built in a mode that
78 is incompatible with \QD, it won't be loaded and
79 installed.
80
81 The header and source files for the widget are declared in the usual way,
82 and we provide an implementation of the plugin interface so that \QD can
83 use the custom widget:
84
85 \snippet examples/designer/customwidgetplugin/customwidgetplugin.pro 3
86
87 It is also important to ensure that the plugin is installed in a
88 location that is searched by \QD. We do this by specifying a
89 target path for the project and adding it to the list of items to
90 install:
91
92 \snippet doc/src/snippets/code/doc_src_examples_customwidgetplugin.qdoc 0
93
94 The custom widget is created as a library, and will be installed
95 alongside the other \QD plugins when the project is installed
96 (using \c{make install} or an equivalent installation procedure).
97 Later, we will ensure that it is recognized as a plugin by \QD by
98 using the Q_EXPORT_PLUGIN2() macro to export the relevant widget
99 information.
100
101 Note that if you want the plugins to appear in a Visual Studio
102 integration, the plugins must be built in release mode and their
103 libraries must be copied into the plugin directory in the install
104 path of the integration (for an example, see \c {C:/program
105 files/trolltech as/visual studio integration/plugins}).
106
107 For more information about plugins, see the \l {How to
108 Create Qt Plugins} documentation.
109
110 \section1 AnalogClock Class Definition and Implementation
111
112 The \c AnalogClock class is defined and implemented in exactly the same
113 way as described in the \l{widgets/analogclock}{Analog Clock example}.
114 Since the class is self-contained, and does not require any external
115 configuration, it can be used without modification as a custom widget in
116 \QD.
117
118 \section1 AnalogClockPlugin Class Definition
119
120 The \c AnalogClock class is exposed to \QD through the \c
121 AnalogClockPlugin class. This class inherits from both QObject and
122 the QDesignerCustomWidgetInterface class, and implements an
123 interface defined by QDesignerCustomWidgetInterface:
124
125 \snippet examples/designer/customwidgetplugin/customwidgetplugin.h 0
126
127 The functions provide information about the widget that \QD can use in
128 the \l{Getting to Know Qt Designer#WidgetBox}{widget box}.
129 The \c initialized private member variable is used to record whether
130 the plugin has been initialized by \QD.
131
132 Note that the only part of the class definition that is specific to
133 this particular custom widget is the class name.
134
135 \section1 AnalogClockPlugin Implementation
136
137 The class constructor simply calls the QObject base class constructor
138 and sets the \c initialized variable to \c false.
139
140 \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 0
141
142 \QD will initialize the plugin when it is required by calling the
143 \c initialize() function:
144
145 \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 1
146
147 In this example, the \c initialized private variable is tested, and only
148 set to \c true if the plugin is not already initialized. Although, this
149 plugin does not require any special code to be executed when it is
150 initialized, we could include such code after the test for initialization.
151
152 The \c isInitialized() function lets \QD know whether the plugin is
153 ready for use:
154
155 \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 2
156
157 Instances of the custom widget are supplied by the \c createWidget()
158 function. The implementation for the analog clock is straightforward:
159
160 \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 3
161
162 In this case, the custom widget only requires a \c parent to be specified.
163 If other arguments need to be supplied to the widget, they can be
164 introduced here.
165
166 The following functions provide information for \QD to use to represent
167 the widget in the widget box.
168 The \c name() function returns the name of class that provides the
169 custom widget:
170
171 \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 4
172
173 The \c group() function is used to describe the type of widget that the
174 custom widget belongs to:
175
176 \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 5
177
178 The widget plugin will be placed in a section identified by its
179 group name in \QD's widget box. The icon used to represent the
180 widget in the widget box is returned by the \c icon() function:
181
182 \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 6
183
184 In this case, we return a null icon to indicate that we have no icon
185 that can be used to represent the widget.
186