[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
[2] | 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
|
---|
[846] | 13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
| 14 | ** written agreement between you and Nokia.
|
---|
[2] | 15 | **
|
---|
[846] | 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.
|
---|
[2] | 21 | **
|
---|
[561] | 22 | ** If you have questions regarding the use of this file, please contact
|
---|
| 23 | ** Nokia at qt-info@nokia.com.
|
---|
[2] | 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 |
|
---|
| 187 | A tool tip and "What's This?" help can be supplied for the custom widget's
|
---|
| 188 | entry in the widget box. The \c toolTip() function should return a short
|
---|
| 189 | message describing the widget:
|
---|
| 190 |
|
---|
| 191 | \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 7
|
---|
| 192 |
|
---|
| 193 | The \c whatsThis() function can return a longer description:
|
---|
| 194 |
|
---|
| 195 | \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 8
|
---|
| 196 |
|
---|
| 197 | The \c isContainer() function tells \QD whether the widget is supposed to
|
---|
| 198 | be used as a container for other widgets. If not, \QD will not allow the
|
---|
| 199 | user to place widgets inside it.
|
---|
| 200 |
|
---|
| 201 | \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 9
|
---|
| 202 |
|
---|
| 203 | Most widgets in Qt can contain child widgets, but it only makes sense
|
---|
| 204 | to use dedicated container widgets for this purpose in \QD. By returning
|
---|
| 205 | \c false, we indicate that the custom widget cannot hold other widgets;
|
---|
| 206 | if we returned true, \QD would allow other widgets to be placed inside
|
---|
| 207 | the analog clock and a layout to be defined.
|
---|
| 208 |
|
---|
| 209 | The \c domXml() function provides a way to include default settings for
|
---|
| 210 | the widget in the standard XML format used by \QD. In this case, we only
|
---|
| 211 | specify the widget's geometry:
|
---|
| 212 |
|
---|
| 213 | \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 10
|
---|
| 214 |
|
---|
| 215 | If the widget provides a reasonable size hint, it is not necessary to
|
---|
| 216 | define it here. In addition, returning an empty string instead of a
|
---|
| 217 | \c{<widget>} element will tell \QD not to install the widget in the
|
---|
| 218 | widget box.
|
---|
| 219 |
|
---|
| 220 | To make the analog clock widget usable by applications, we implement
|
---|
| 221 | the \c includeFile() function to return the name of the header file
|
---|
| 222 | containing the custom widget class definition:
|
---|
| 223 |
|
---|
| 224 | \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 12
|
---|
| 225 |
|
---|
| 226 | Finally, we use the Q_EXPORT_PLUGIN2() macro to export the \c
|
---|
| 227 | AnalogClockPlugin class for use with \QD:
|
---|
| 228 |
|
---|
| 229 | \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 13
|
---|
| 230 |
|
---|
| 231 | This macro ensures that \QD can access and construct the custom widget.
|
---|
| 232 | Without this macro, there is no way for \QD to use the widget.
|
---|
| 233 |
|
---|
| 234 | It is important to note that you can only use the Q_EXPORT_PLUGIN2()
|
---|
| 235 | macro once in any implementation. If you have several custom widgets in
|
---|
| 236 | an implementation that you wish to make available to \QD, you will need
|
---|
| 237 | to implement \l{QDesignerCustomWidgetCollectionInterface}.
|
---|
| 238 | */
|
---|