summaryrefslogtreecommitdiffstats
path: root/examples/designer/doc/src/customwidgetplugin.qdoc
blob: 82b70cea3aac93066f5f1767d7545169e9427ff9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \example customwidgetplugin
    \ingroup examples-designer
    \title Custom Widget Plugin Example

    \brief Creating a custom widget plugin for Qt Designer.

    \image customwidgetplugin-example.png

    In this example, the custom widget used is based on the
    \l{widgets/analogclock}{Analog Clock example}, and does not provide any custom
    signals or slots.

    \section1 Preparation

    To provide a custom widget that can be used with \QD, we need to supply a
    self-contained implementation and provide a plugin interface. In this
    example, we reuse the \l{widgets/analogclock}{Analog Clock example} for
    convenience.

    Since custom widgets plugins rely on components supplied with \QD, the
    project file that we use needs to contain information about \QD's
    library components:

    \snippet customwidgetplugin/customwidgetplugin.pro 2
    \snippet customwidgetplugin/customwidgetplugin.pro 0

    The \c TEMPLATE variable's value makes \c qmake create the custom
    widget as a library. Later, we will ensure that the widget will be
    recognized as a plugin by Qt by using the Q_PLUGIN_METADATA() macro
    to export the relevant widget information.


    The \c CONFIG variable is set to \c plugin, which ensures that \c qmake
    considers the custom widget a plugin library.

    The \c QT variable contains the keyword \c uiplugin. This plugin type
    provides a factory function for custom widget creation by implementing
    the abstract interfaces QDesignerCustomWidgetInterface or
    QDesignerCustomWidgetCollectionInterface, suitable for use with
    QUiLoader. It does not have a dependency on the \QD libraries.
    Plugins accessing other interfaces of \QD to implement container extensions
    or other \QD specific functionality follow different rules and are covered
    by other examples.

    The header and source files for the widget are declared in the usual way,
    and we provide an implementation of the plugin interface so that \QD can
    use the custom widget:

    \snippet customwidgetplugin/customwidgetplugin.pro 3

    It is also important to ensure that the plugin is installed in a
    location that is searched by \QD. We do this by specifying a
    target path for the project and adding it to the list of items to
    install:

    \snippet doc/snippets/doc_src_examples_customwidgetplugin.pro 0

    The custom widget is created as a library, and will be installed
    alongside the other \QD plugins when the project is installed
    (using \c{make install} or an equivalent installation procedure).
    Later, we will ensure that it is recognized as a plugin by \QD by
    using the Q_PLUGIN_METADATA() macro to export the relevant widget
    information.

    Note that if you want the plugins to appear in a Visual Studio
    integration, the plugins must be built in release mode and their
    libraries must be copied into the plugin directory in the install
    path of the integration (for an example, see \c {C:/program
    files/trolltech as/visual studio integration/plugins}).

    For more information about plugins, see the \l {How to
    Create Qt Plugins} documentation.

    \section1 AnalogClock Class Definition and Implementation

    The \c AnalogClock class is defined and implemented in exactly the same
    way as described in the \l{widgets/analogclock}{Analog Clock example}.
    Since the class is self-contained, and does not require any external
    configuration, it can be used without modification as a custom widget in
    \QD.

    \section1 AnalogClockPlugin Class Definition

    The \c AnalogClock class is exposed to \QD through the \c
    AnalogClockPlugin class. This class inherits from both QObject and
    the QDesignerCustomWidgetInterface class, and implements an
    interface defined by QDesignerCustomWidgetInterface:

    \snippet customwidgetplugin/customwidgetplugin.h 0

    The functions provide information about the widget that \QD can use in
    the \l{Getting to Know Qt Designer#WidgetBox}{widget box}.
    The \c initialized private member variable is used to record whether
    the plugin has been initialized by \QD.

    Note that the only part of the class definition that is specific to
    this particular custom widget is the class name.

    \section1 AnalogClockPlugin Implementation

    The class constructor simply calls the QObject base class constructor
    and sets the \c initialized variable to \c false.

    \snippet customwidgetplugin/customwidgetplugin.cpp 0

    \QD will initialize the plugin when it is required by calling the
    \c initialize() function:

    \snippet customwidgetplugin/customwidgetplugin.cpp 1

    In this example, the \c initialized private variable is tested, and only
    set to \c true if the plugin is not already initialized. Although, this
    plugin does not require any special code to be executed when it is
    initialized, we could include such code after the test for initialization.

    The \c isInitialized() function lets \QD know whether the plugin is
    ready for use:

    \snippet customwidgetplugin/customwidgetplugin.cpp 2

    Instances of the custom widget are supplied by the \c createWidget()
    function. The implementation for the analog clock is straightforward:

    \snippet customwidgetplugin/customwidgetplugin.cpp 3

    In this case, the custom widget only requires a \c parent to be specified.
    If other arguments need to be supplied to the widget, they can be
    introduced here.

    The following functions provide information for \QD to use to represent
    the widget in the widget box.
    The \c name() function returns the name of class that provides the
    custom widget:

    \snippet customwidgetplugin/customwidgetplugin.cpp 4

    The \c group() function is used to describe the type of widget that the
    custom widget belongs to:

    \snippet customwidgetplugin/customwidgetplugin.cpp 5

    The widget plugin will be placed in a section identified by its
    group name in \QD's widget box. The icon used to represent the
    widget in the widget box is returned by the \c icon() function:

    \snippet customwidgetplugin/customwidgetplugin.cpp 6

    In this case, we return a null icon to indicate that we have no icon
    that can be used to represent the widget.

    A tool tip and "What's This?" help can be supplied for the custom widget's
    entry in the widget box. The \c toolTip() function should return a short
    message describing the widget:

    \snippet customwidgetplugin/customwidgetplugin.cpp 7

    The \c whatsThis() function can return a longer description:

    \snippet customwidgetplugin/customwidgetplugin.cpp 8

    The \c isContainer() function tells \QD whether the widget is supposed to
    be used as a container for other widgets. If not, \QD will not allow the
    user to place widgets inside it.

    \snippet customwidgetplugin/customwidgetplugin.cpp 9

    Most widgets in Qt can contain child widgets, but it only makes sense
    to use dedicated container widgets for this purpose in \QD. By returning
    \c false, we indicate that the custom widget cannot hold other widgets;
    if we returned true, \QD would allow other widgets to be placed inside
    the analog clock and a layout to be defined.

    The \c domXml() function provides a way to include default settings for
    the widget in the standard XML format used by \QD. In this case, we only
    specify the widget's geometry:

    \snippet customwidgetplugin/customwidgetplugin.cpp 10

    If the widget provides a reasonable size hint, it is not necessary to
    define it here. In addition, returning an empty string instead of a
    \c{<widget>} element will tell \QD not to install the widget in the
    widget box.

    To make the analog clock widget usable by applications, we implement
    the \c includeFile() function to return the name of the header file
    containing the custom widget class definition:

    \snippet customwidgetplugin/customwidgetplugin.cpp 12
*/