/usr/share/qt5/doc/qtdesigner/qtdesigner-customwidgetplugin-example.html is in qttools5-doc-html 5.2.1-8build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- customwidgetplugin.qdoc -->
<title>Custom Widget Plugin Example | QtDesigner </title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<ul>
<li>Qt 5.2</li>
<li><a href="qtdesigner-manual.html">Qt Designer Manual</a></li>
<li>Custom Widget Plugin Example</li>
<li id="buildversion">
Qt 5.2.1 Reference Documentation</li>
</ul>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#preparation">Preparation</a></li>
<li class="level1"><a href="#analogclock-class-definition-and-implementation">AnalogClock Class Definition and Implementation</a></li>
<li class="level1"><a href="#analogclockplugin-class-definition">AnalogClockPlugin Class Definition</a></li>
<li class="level1"><a href="#analogclockplugin-implementation">AnalogClockPlugin Implementation</a></li>
</ul>
</div>
<h1 class="title">Custom Widget Plugin Example</h1>
<span class="subtitle"></span>
<!-- $$$customwidgetplugin-description -->
<div class="descr"> <a name="details"></a>
<p>Creating a custom widget plugin for Qt Designer.<p class="centerAlign"><img src="images/customwidgetplugin-example.png" alt="" /></p><p>In this example, the custom widget used is based on the <a href="../qtwidgets/qtwidgets-widgets-analogclock-example.html">Analog Clock example</a>, and does not provide any custom signals or slots.</p>
<a name="preparation"></a>
<h2>Preparation</h2>
<p>To provide a custom widget that can be used with <i>Qt Designer</i>, we need to supply a self-contained implementation and provide a plugin interface. In this example, we reuse the <a href="../qtwidgets/qtwidgets-widgets-analogclock-example.html">Analog Clock example</a> for convenience.</p>
<p>Since custom widgets plugins rely on components supplied with <i>Qt Designer</i>, the project file that we use needs to contain information about <i>Qt Designer</i>'s library components:</p>
<pre class="cpp">CONFIG += plugin
TEMPLATE = lib
QT += widgets designer</pre>
<p>The <tt>TEMPLATE</tt> variable's value makes <tt>qmake</tt> 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 <a href="../qtcore/qtplugin.html#Q_PLUGIN_METADATA">Q_PLUGIN_METADATA</a>() macro to export the relevant widget information.</p>
<p>The <tt>CONFIG</tt> variable contains two values, <tt>designer</tt> and <tt>plugin</tt>:</p>
<ul>
<li><tt>designer</tt>: Since custom widgets plugins rely on components supplied with <i>Qt Designer</i>, this value ensures that our plugin links against <i>Qt Designer</i>'s library (<tt>libQtDesigner.so</tt>).</li>
<li><tt>plugin</tt>: We also need to ensure that <tt>qmake</tt> considers the custom widget a plugin library.</li>
</ul>
<p>When Qt is configured to build in both debug and release modes, <i>Qt Designer</i> will be built in release mode. When this occurs, it is necessary to ensure that plugins are also built in release mode. For that reason we add the <tt>debug_and_release</tt> value to the <tt>CONFIG</tt> variable. Otherwise, if a plugin is built in a mode that is incompatible with <i>Qt Designer</i>, it won't be loaded and installed.</p>
<p>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 <i>Qt Designer</i> can use the custom widget:</p>
<pre class="cpp">HEADERS = analogclock.h \
customwidgetplugin.h
SOURCES = analogclock.cpp \
customwidgetplugin.cpp
OTHER_FILES += analogclock.json</pre>
<p>It is also important to ensure that the plugin is installed in a location that is searched by <i>Qt Designer</i>. We do this by specifying a target path for the project and adding it to the list of items to install:</p>
<pre class="cpp">target.path = $$[QT_INSTALL_PLUGINS]/designer
INSTALLS += target</pre>
<p>The custom widget is created as a library, and will be installed alongside the other <i>Qt Designer</i> plugins when the project is installed (using <tt>make install</tt> or an equivalent installation procedure). Later, we will ensure that it is recognized as a plugin by <i>Qt Designer</i> by using the <a href="../qtcore/qtplugin.html#Q_PLUGIN_METADATA">Q_PLUGIN_METADATA</a>() macro to export the relevant widget information.</p>
<p>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 <tt>C:/program files/trolltech as/visual studio integration/plugins</tt>).</p>
<p>For more information about plugins, see the <a href="../qtcore/plugins-howto.html">How to Create Qt Plugins</a> documentation.</p>
<a name="analogclock-class-definition-and-implementation"></a>
<h2>AnalogClock Class Definition and Implementation</h2>
<p>The <tt>AnalogClock</tt> class is defined and implemented in exactly the same way as described in the <a href="../qtwidgets/qtwidgets-widgets-analogclock-example.html">Analog Clock example</a>. Since the class is self-contained, and does not require any external configuration, it can be used without modification as a custom widget in <i>Qt Designer</i>.</p>
<a name="analogclockplugin-class-definition"></a>
<h2>AnalogClockPlugin Class Definition</h2>
<p>The <tt>AnalogClock</tt> class is exposed to <i>Qt Designer</i> through the <tt>AnalogClockPlugin</tt> class. This class inherits from both <a href="../qtcore/qobject.html">QObject</a> and the <a href="qdesignercustomwidgetinterface.html">QDesignerCustomWidgetInterface</a> class, and implements an interface defined by <a href="qdesignercustomwidgetinterface.html">QDesignerCustomWidgetInterface</a>:</p>
<pre class="cpp"><span class="keyword">class</span> AnalogClockPlugin : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span><span class="operator">,</span> <span class="keyword">public</span> <span class="type"><a href="qdesignercustomwidgetinterface.html">QDesignerCustomWidgetInterface</a></span>
{
Q_OBJECT
Q_PLUGIN_METADATA(IID <span class="string">"org.qt-project.Qt.QDesignerCustomWidgetInterface"</span> FILE <span class="string">"analogclock.json"</span>)
Q_INTERFACES(<span class="type"><a href="qdesignercustomwidgetinterface.html">QDesignerCustomWidgetInterface</a></span>)
<span class="keyword">public</span>:
AnalogClockPlugin(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
bool isContainer() <span class="keyword">const</span>;
bool isInitialized() <span class="keyword">const</span>;
<span class="type">QIcon</span> icon() <span class="keyword">const</span>;
<span class="type"><a href="../qtcore/qstring.html">QString</a></span> domXml() <span class="keyword">const</span>;
<span class="type"><a href="../qtcore/qstring.html">QString</a></span> group() <span class="keyword">const</span>;
<span class="type"><a href="../qtcore/qstring.html">QString</a></span> includeFile() <span class="keyword">const</span>;
<span class="type"><a href="../qtcore/qstring.html">QString</a></span> name() <span class="keyword">const</span>;
<span class="type"><a href="../qtcore/qstring.html">QString</a></span> toolTip() <span class="keyword">const</span>;
<span class="type"><a href="../qtcore/qstring.html">QString</a></span> whatsThis() <span class="keyword">const</span>;
<span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>createWidget(<span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent);
<span class="type">void</span> initialize(<span class="type"><a href="qdesignerformeditorinterface.html">QDesignerFormEditorInterface</a></span> <span class="operator">*</span>core);
<span class="keyword">private</span>:
bool initialized;
};</pre>
<p>The functions provide information about the widget that <i>Qt Designer</i> can use in the <a href="designer-to-know.html#widgetbox">widget box</a>. The <tt>initialized</tt> private member variable is used to record whether the plugin has been initialized by <i>Qt Designer</i>.</p>
<p>Note that the only part of the class definition that is specific to this particular custom widget is the class name.</p>
<a name="analogclockplugin-implementation"></a>
<h2>AnalogClockPlugin Implementation</h2>
<p>The class constructor simply calls the <a href="../qtcore/qobject.html">QObject</a> base class constructor and sets the <tt>initialized</tt> variable to <tt>false</tt>.</p>
<pre class="cpp">AnalogClockPlugin<span class="operator">::</span>AnalogClockPlugin(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent)
: <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>(parent)
{
initialized <span class="operator">=</span> <span class="keyword">false</span>;
}</pre>
<p><i>Qt Designer</i> will initialize the plugin when it is required by calling the <tt>initialize()</tt> function:</p>
<pre class="cpp"><span class="type">void</span> AnalogClockPlugin<span class="operator">::</span>initialize(<span class="type"><a href="qdesignerformeditorinterface.html">QDesignerFormEditorInterface</a></span> <span class="operator">*</span> <span class="comment">/* core */</span>)
{
<span class="keyword">if</span> (initialized)
<span class="keyword">return</span>;
initialized <span class="operator">=</span> <span class="keyword">true</span>;
}</pre>
<p>In this example, the <tt>initialized</tt> private variable is tested, and only set to <tt>true</tt> 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.</p>
<p>The <tt>isInitialized()</tt> function lets <i>Qt Designer</i> know whether the plugin is ready for use:</p>
<pre class="cpp">bool AnalogClockPlugin<span class="operator">::</span>isInitialized() <span class="keyword">const</span>
{
<span class="keyword">return</span> initialized;
}</pre>
<p>Instances of the custom widget are supplied by the <tt>createWidget()</tt> function. The implementation for the analog clock is straightforward:</p>
<pre class="cpp"><span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>AnalogClockPlugin<span class="operator">::</span>createWidget(<span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
{
<span class="keyword">return</span> <span class="keyword">new</span> AnalogClock(parent);
}</pre>
<p>In this case, the custom widget only requires a <tt>parent</tt> to be specified. If other arguments need to be supplied to the widget, they can be introduced here.</p>
<p>The following functions provide information for <i>Qt Designer</i> to use to represent the widget in the widget box. The <tt>name()</tt> function returns the name of class that provides the custom widget:</p>
<pre class="cpp"><span class="type"><a href="../qtcore/qstring.html">QString</a></span> AnalogClockPlugin<span class="operator">::</span>name() <span class="keyword">const</span>
{
<span class="keyword">return</span> <span class="string">"AnalogClock"</span>;
}</pre>
<p>The <tt>group()</tt> function is used to describe the type of widget that the custom widget belongs to:</p>
<pre class="cpp"><span class="type"><a href="../qtcore/qstring.html">QString</a></span> AnalogClockPlugin<span class="operator">::</span>group() <span class="keyword">const</span>
{
<span class="keyword">return</span> <span class="string">"Display Widgets [Examples]"</span>;
}</pre>
<p>The widget plugin will be placed in a section identified by its group name in <i>Qt Designer</i>'s widget box. The icon used to represent the widget in the widget box is returned by the <tt>icon()</tt> function:</p>
<pre class="cpp"><span class="type">QIcon</span> AnalogClockPlugin<span class="operator">::</span>icon() <span class="keyword">const</span>
{
<span class="keyword">return</span> <span class="type">QIcon</span>();
}</pre>
<p>In this case, we return a null icon to indicate that we have no icon that can be used to represent the widget.</p>
<p>A tool tip and "What's This?" help can be supplied for the custom widget's entry in the widget box. The <tt>toolTip()</tt> function should return a short message describing the widget:</p>
<pre class="cpp"><span class="type"><a href="../qtcore/qstring.html">QString</a></span> AnalogClockPlugin<span class="operator">::</span>toolTip() <span class="keyword">const</span>
{
<span class="keyword">return</span> <span class="string">""</span>;
}</pre>
<p>The <tt>whatsThis()</tt> function can return a longer description:</p>
<pre class="cpp"><span class="type"><a href="../qtcore/qstring.html">QString</a></span> AnalogClockPlugin<span class="operator">::</span>whatsThis() <span class="keyword">const</span>
{
<span class="keyword">return</span> <span class="string">""</span>;
}</pre>
<p>The <tt>isContainer()</tt> function tells <i>Qt Designer</i> whether the widget is supposed to be used as a container for other widgets. If not, <i>Qt Designer</i> will not allow the user to place widgets inside it.</p>
<pre class="cpp">bool AnalogClockPlugin<span class="operator">::</span>isContainer() <span class="keyword">const</span>
{
<span class="keyword">return</span> <span class="keyword">false</span>;
}</pre>
<p>Most widgets in Qt can contain child widgets, but it only makes sense to use dedicated container widgets for this purpose in <i>Qt Designer</i>. By returning <tt>false</tt>, we indicate that the custom widget cannot hold other widgets; if we returned true, <i>Qt Designer</i> would allow other widgets to be placed inside the analog clock and a layout to be defined.</p>
<p>The <tt>domXml()</tt> function provides a way to include default settings for the widget in the standard XML format used by <i>Qt Designer</i>. In this case, we only specify the widget's geometry:</p>
<pre class="cpp"><span class="type"><a href="../qtcore/qstring.html">QString</a></span> AnalogClockPlugin<span class="operator">::</span>domXml() <span class="keyword">const</span>
{
<span class="keyword">return</span> <span class="string">"<ui language=\"c++\">\n"</span>
<span class="string">" <widget class=\"AnalogClock\" name=\"analogClock\">\n"</span>
<span class="string">" <property name=\"geometry\">\n"</span>
<span class="string">" <rect>\n"</span>
<span class="string">" <x>0</x>\n"</span>
<span class="string">" <y>0</y>\n"</span>
<span class="string">" <width>100</width>\n"</span>
<span class="string">" <height>100</height>\n"</span>
<span class="string">" </rect>\n"</span>
<span class="string">" </property>\n"</span>
<span class="string">" <property name=\"toolTip\" >\n"</span>
<span class="string">" <string>The current time</string>\n"</span>
<span class="string">" </property>\n"</span>
<span class="string">" <property name=\"whatsThis\" >\n"</span>
<span class="string">" <string>The analog clock widget displays the current time.</string>\n"</span>
<span class="string">" </property>\n"</span>
<span class="string">" </widget>\n"</span>
<span class="string">"</ui>\n"</span>;
}</pre>
<p>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 <tt><widget></tt> element will tell <i>Qt Designer</i> not to install the widget in the widget box.</p>
<p>To make the analog clock widget usable by applications, we implement the <tt>includeFile()</tt> function to return the name of the header file containing the custom widget class definition:</p>
<pre class="cpp"><span class="type"><a href="../qtcore/qstring.html">QString</a></span> AnalogClockPlugin<span class="operator">::</span>includeFile() <span class="keyword">const</span>
{
<span class="keyword">return</span> <span class="string">"analogclock.h"</span>;
}</pre>
<p>Files:</p>
<ul>
<li><a href="qtdesigner-customwidgetplugin-analogclock-cpp.html">customwidgetplugin/analogclock.cpp</a></li>
<li><a href="qtdesigner-customwidgetplugin-analogclock-h.html">customwidgetplugin/analogclock.h</a></li>
<li><a href="qtdesigner-customwidgetplugin-customwidgetplugin-cpp.html">customwidgetplugin/customwidgetplugin.cpp</a></li>
<li><a href="qtdesigner-customwidgetplugin-customwidgetplugin-h.html">customwidgetplugin/customwidgetplugin.h</a></li>
<li><a href="qtdesigner-customwidgetplugin-customwidgetplugin-pro.html">customwidgetplugin/customwidgetplugin.pro</a></li>
</ul>
</div>
<!-- @@@customwidgetplugin -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2013 Digia Plc and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|