1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
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 qt-info@nokia.com.
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \example designer/worldtimeclockplugin
|
---|
30 | \title World Time Clock Plugin Example
|
---|
31 |
|
---|
32 | The World Time Clock Plugin example shows how to create a custom
|
---|
33 | widget plugin for \QD that uses signals and slots.
|
---|
34 |
|
---|
35 | \image worldtimeclockplugin-example.png
|
---|
36 |
|
---|
37 | In this example, we simply extend the \l
|
---|
38 | {designer/customwidgetplugin}{Custom Widget Plugin} example and
|
---|
39 | its custom widget (based on the \l{widgets/analogclock}{Analog
|
---|
40 | Clock} example), by introducing the concept of signals and slots.
|
---|
41 |
|
---|
42 | The World Time Clock Plugin example consists of two classes:
|
---|
43 |
|
---|
44 | \list
|
---|
45 | \o \c WorldTimeClock is a custom clock widget with hour and
|
---|
46 | minute hands that is automatically updated every few seconds.
|
---|
47 | \o \c WorldTimeClockPlugin exposes the \c WorldTimeClock class to \QD.
|
---|
48 | \endlist
|
---|
49 |
|
---|
50 | First we will take a look at the \c WorldTimeClock class which
|
---|
51 | extends the \l {designer/customwidgetplugin}{Custom Widget Plugin}
|
---|
52 | example's \c AnalogClock class by providing a signal and a
|
---|
53 | slot. Then we will take a quick look at the \c
|
---|
54 | WorldTimeClockPlugin class, but this class is in most parts
|
---|
55 | identical to the \l {designer/customwidgetplugin}{Custom Widget
|
---|
56 | Plugin} example's implementation.
|
---|
57 |
|
---|
58 | Finally we take a look at the plugin's project file. The project
|
---|
59 | file for custom widget plugins needs some additional information
|
---|
60 | to ensure that they will work within \QD. This is also covered in
|
---|
61 | the \l {designer/customwidgetplugin}{Custom Widget Plugin} example,
|
---|
62 | but due to its importance (custom widget plugins rely on
|
---|
63 | components supplied with \QD which must be specified in the
|
---|
64 | project file that we use) we will repeat it here.
|
---|
65 |
|
---|
66 | \section1 WorldTimeClock Class
|
---|
67 |
|
---|
68 | The \c WorldTimeClock class inherits QWidget, and is a custom
|
---|
69 | clock widget with hour and minute hands that is automatically
|
---|
70 | updated every few seconds. What makes this example different from
|
---|
71 | the \l {designer/customwidgetplugin}{Custom Widget Plugin}
|
---|
72 | example, is the introduction of the signal and slot in the custom
|
---|
73 | widget class:
|
---|
74 |
|
---|
75 | \snippet examples/designer/worldtimeclockplugin/worldtimeclock.h 1
|
---|
76 |
|
---|
77 | Note the use of the QDESIGNER_WIDGET_EXPORT macro. This is needed
|
---|
78 | to ensure that \QD can create instances of the widget on some
|
---|
79 | platforms, but it is a good idea to use it on all platforms.
|
---|
80 |
|
---|
81 | We declare the \c setTimeZone() slot with an associated \c
|
---|
82 | timeZoneOffset variable, and we declare an \c updated() signal
|
---|
83 | which takes the current time as argument and is emitted whenever
|
---|
84 | the widget is repainted.
|
---|
85 |
|
---|
86 | \image worldtimeclock-connection.png
|
---|
87 |
|
---|
88 | In \QD's workspace we can then, for example, connect the \c
|
---|
89 | WorldTimeClock widget's \c updated() signal to a QTimeEdit's \l
|
---|
90 | {QDateTimeEdit::setTime()}{setTime()} slot using \QD's mode
|
---|
91 | for editing signal and slots.
|
---|
92 |
|
---|
93 | \image worldtimeclock-signalandslot.png
|
---|
94 |
|
---|
95 | We can also connect a QSpinBox's \l
|
---|
96 | {QSpinBox::valueChanged()}{valueChanged()} signal to the \c
|
---|
97 | WorldTimeClock's \c setTimeZone() slot.
|
---|
98 |
|
---|
99 | \section1 WorldTimeClockPlugin Class
|
---|
100 |
|
---|
101 | The \c WorldTimeClockPlugin class exposes the \c WorldTimeClock
|
---|
102 | class to \QD. Its definition is equivalent to the \l
|
---|
103 | {designer/customwidgetplugin}{Custom Widget Plugin} example's
|
---|
104 | plugin class which is explained in detail. The only part of the
|
---|
105 | class definition that is specific to this particular custom widget
|
---|
106 | is the class name:
|
---|
107 |
|
---|
108 | \snippet examples/designer/worldtimeclockplugin/worldtimeclockplugin.h 0
|
---|
109 |
|
---|
110 | The plugin class provides \QD with basic information about our
|
---|
111 | plugin, such as its class name and its include file. Furthermore
|
---|
112 | it knows how to create instances of the \c WorldTimeClockPlugin
|
---|
113 | widget. \c WorldTimeClockPlugin also defines the \l
|
---|
114 | {QDesignerCustomWidgetInterface::initialize()}{initialize()}
|
---|
115 | function which is called after the plugin is loaded into \QD. The
|
---|
116 | function's QDesignerFormEditorInterface parameter provides the
|
---|
117 | plugin with a gateway to all of \QD's API's.
|
---|
118 |
|
---|
119 | The \c WorldTimeClockPlugin class inherits from both QObject and
|
---|
120 | QDesignerCustomWidgetInterface. It is important to remember, when
|
---|
121 | using multiple inheritance, to ensure that all the interfaces
|
---|
122 | (i.e. the classes that doesn't inherit Q_OBJECT) are made known to
|
---|
123 | the meta object system using the Q_INTERFACES() macro. This
|
---|
124 | enables \QD to use \l qobject_cast() to query for supported
|
---|
125 | interfaces using nothing but a QObject pointer.
|
---|
126 |
|
---|
127 | The implementation of the \c WorldTimeClockPlugin is also
|
---|
128 | equivalent to the plugin interface implementation in the \l
|
---|
129 | {designer/customwidgetplugin}{Custom Widget Plugin} example (only
|
---|
130 | the class name and the implementation of
|
---|
131 | QDesignerCustomWidgetInterface::domXml() differ). The main thing
|
---|
132 | to remember is to use the Q_EXPORT_PLUGIN2() macro to export the \c
|
---|
133 | WorldTimeClockPlugin class for use with \QD:
|
---|
134 |
|
---|
135 | \snippet examples/designer/worldtimeclockplugin/worldtimeclockplugin.cpp 0
|
---|
136 |
|
---|
137 | Without this macro, there is no way for Qt Designer to use the
|
---|
138 | widget.
|
---|
139 |
|
---|
140 | \section1 The Project File: worldtimeclockplugin.pro
|
---|
141 |
|
---|
142 | The project file for custom widget plugins needs some additional
|
---|
143 | information to ensure that they will work as expected within \QD:
|
---|
144 |
|
---|
145 | \snippet examples/designer/worldtimeclockplugin/worldtimeclockplugin.pro 0
|
---|
146 | \snippet examples/designer/worldtimeclockplugin/worldtimeclockplugin.pro 1
|
---|
147 |
|
---|
148 | The \c TEMPLATE variable's value make \c qmake create the custom
|
---|
149 | widget as a library. The \c CONFIG variable contains two values,
|
---|
150 | \c designer and \c plugin:
|
---|
151 |
|
---|
152 | \list
|
---|
153 | \o \c designer: Since custom widgets plugins rely on components
|
---|
154 | supplied with \QD, this value ensures that our plugin links against
|
---|
155 | \QD's library (\c libQtDesigner.so).
|
---|
156 |
|
---|
157 | \o \c plugin: We also need to ensure that \c qmake considers the
|
---|
158 | custom widget a \e plugin library.
|
---|
159 | \endlist
|
---|
160 |
|
---|
161 | When Qt is configured to build in both debug and release modes,
|
---|
162 | \QD will be built in release mode. When this occurs, it is
|
---|
163 | necessary to ensure that plugins are also built in release
|
---|
164 | mode. For that reason you might have to add a \c release value to
|
---|
165 | your \c CONFIG variable. Otherwise, if a plugin is built in a mode
|
---|
166 | that is incompatible with \QD, it won't be loaded and
|
---|
167 | installed.
|
---|
168 |
|
---|
169 | The header and source files for the widget are declared in the
|
---|
170 | usual way, and in addition we provide an implementation of the
|
---|
171 | plugin interface so that \QD can use the custom widget.
|
---|
172 |
|
---|
173 | \snippet examples/designer/worldtimeclockplugin/worldtimeclockplugin.pro 2
|
---|
174 |
|
---|
175 | It is important to ensure that the plugin is installed in a location that
|
---|
176 | is searched by \QD. We do this by specifying a target path for the project
|
---|
177 | and adding it to the list of items to install:
|
---|
178 |
|
---|
179 | \snippet doc/src/snippets/code/doc_src_examples_worldtimeclockplugin.qdoc 0
|
---|
180 |
|
---|
181 | The custom widget is created as a library, and will be installed
|
---|
182 | alongside the other \QD plugins when the project is installed
|
---|
183 | (using \c{make install} or an equivalent installation procedure).
|
---|
184 | Later, we will ensure that it is recognized as a plugin by \QD by
|
---|
185 | using the Q_EXPORT_PLUGIN2() macro to export the relevant widget
|
---|
186 | information.
|
---|
187 |
|
---|
188 | Note that if you want the plugins to appear in a Visual Studio
|
---|
189 | integration, the plugins must be built in release mode and their
|
---|
190 | libraries must be copied into the plugin directory in the install
|
---|
191 | path of the integration (for an example, see \c {C:/program
|
---|
192 | files/trolltech as/visual studio integration/plugins}).
|
---|
193 |
|
---|
194 | For more information about plugins, see the \l {How to Create Qt
|
---|
195 | Plugins} document.
|
---|
196 | */
|
---|