This file is indexed.

/usr/share/qt5/doc/qtdesigner/designer-creating-custom-widgets.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
<?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" />
<!-- designer-manual.qdoc -->
  <title>Creating Custom Widgets for Qt Designer | 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>Creating Custom Widgets for Qt Designer</li>
<li id="buildversion">
Qt 5.2.1 Reference Documentation</li>
    </ul>
    </div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="designer-using-custom-widgets.html" />
  <link rel="next" href="designer-creating-custom-widgets-extensions.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="designer-using-custom-widgets.html">Using Custom Widgets with Qt Designer</a>
<a class="nextPage" href="designer-creating-custom-widgets-extensions.html">Creating Custom Widget Extensions</a>
</p><p/>
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#getting-started">Getting Started</a></li>
<li class="level2"><a href="#providing-an-interface-description">Providing an Interface Description</a></li>
<li class="level2"><a href="#notes-on-the-func-target-domxml-domxml-func-function">Notes on the <tt>domXml()</tt> Function</a></li>
<li class="level1"><a href="#plugin-requirements">Plugin Requirements</a></li>
<li class="level1"><a href="#creating-well-behaved-widgets">Creating Well Behaved Widgets</a></li>
<li class="level1"><a href="#building-and-installing-the-plugin">Building and Installing the Plugin</a></li>
<li class="level2"><a href="#a-simple-plugin">A Simple Plugin</a></li>
<li class="level2"><a href="#splitting-up-the-plugin">Splitting up the Plugin</a></li>
<li class="level1"><a href="#related-examples">Related Examples</a></li>
</ul>
</div>
<h1 class="title">Creating Custom Widgets for Qt Designer</h1>
<span class="subtitle"></span>
<!-- $$$designer-creating-custom-widgets.html-description -->
<div class="descr"> <a name="details"></a>
<p><i>Qt Designer</i>'s plugin-based architecture allows user-defined and third party custom widgets to be edited just like you do with standard Qt widgets. All of the custom widget's features are made available to <i>Qt Designer</i>, including widget properties, signals, and slots. Since <i>Qt Designer</i> uses real widgets during the form design process, custom widgets will appear the same as they do when previewed.</p>
<p class="centerAlign"><img src="images/worldtimeclockplugin-example.png" alt="" /></p><p>The <a href="qtdesigner-module.html">QtDesigner</a> module provides you with the ability to create custom widgets in <i>Qt Designer</i>.</p>
<a name="getting-started"></a>
<h2>Getting Started</h2>
<p>To integrate a custom widget with <i>Qt Designer</i>, you require a suitable description for the widget and an appropriate <tt>.pro</tt> file.</p>
<a name="providing-an-interface-description"></a>
<h3>Providing an Interface Description</h3>
<p>To inform <i>Qt Designer</i> about the type of widget you want to provide, create a subclass of <a href="qdesignercustomwidgetinterface.html">QDesignerCustomWidgetInterface</a> that describes the various properties your widget exposes. Most of these are supplied by functions that are pure virtual in the base class, because only the author of the plugin can provide this information.</p>
<table class="generic">
 <thead><tr class="qt-style"><th >Function</th><th >Description of the return value</th></tr></thead>
<tr valign="top" class="odd"><td ><tt>name()</tt></td><td >The name of the class that provides the widget.</td></tr>
<tr valign="top" class="even"><td ><tt>group()</tt></td><td >The group in <i>Qt Designer</i>'s widget box that the widget belongs to.</td></tr>
<tr valign="top" class="odd"><td ><tt>toolTip()</tt></td><td >A short description to help users identify the widget in <i>Qt Designer</i>.</td></tr>
<tr valign="top" class="even"><td ><tt>whatsThis()</tt></td><td >A longer description of the widget for users of <i>Qt Designer</i>.</td></tr>
<tr valign="top" class="odd"><td ><tt>includeFile()</tt></td><td >The header file that must be included in applications that use this widget. This information is stored in UI files and will be used by <tt>uic</tt> to create a suitable <tt>#includes</tt> statement in the code it generates for the form containing the custom widget.</td></tr>
<tr valign="top" class="even"><td ><tt>icon()</tt></td><td >An icon that can be used to represent the widget in <i>Qt Designer</i>'s widget box.</td></tr>
<tr valign="top" class="odd"><td ><tt>isContainer()</tt></td><td >True if the widget will be used to hold child widgets; false otherwise.</td></tr>
<tr valign="top" class="even"><td ><tt>createWidget()</tt></td><td >A <a href="../qtwidgets/qwidget.html">QWidget</a> pointer to an instance of the custom widget, constructed with the parent supplied.<p><b>Note: </b>createWidget() is a factory function responsible for creating the widget only. The custom widget's properties will not be available until load() returns.</p></td></tr>
<tr valign="top" class="odd"><td ><tt>domXml()</tt></td><td >A description of the widget's properties, such as its object name, size hint, and other standard <a href="../qtwidgets/qwidget.html">QWidget</a> properties.</td></tr>
<tr valign="top" class="even"><td ><tt>codeTemplate()</tt></td><td >This function is reserved for future use by <i>Qt Designer</i>.</td></tr>
</table>
<p>Two other virtual functions can also be reimplemented:</p>
<table class="generic">
 <tr valign="top" class="odd"><td ><tt>initialize()</tt></td><td >Sets up extensions and other features for custom widgets. Custom container extensions (see <a href="qdesignercontainerextension.html">QDesignerContainerExtension</a>) and task menu extensions (see <a href="qdesignertaskmenuextension.html">QDesignerTaskMenuExtension</a>) should be set up in this function.</td></tr>
<tr valign="top" class="even"><td ><tt>isInitialized()</tt></td><td >Returns true if the widget has been initialized; returns false otherwise. Reimplementations usually check whether the <tt>initialize()</tt> function has been called and return the result of this test.</td></tr>
</table>
<a name="notes-on-the-func-target-domxml-domxml-func-function"></a>
<h3>Notes on the <tt>domXml()</tt> Function</h3>
<p>The <tt>domXml()</tt> function returns a UI file snippet that is used by <i>Qt Designer</i>'s widget factory to create a custom widget and its applicable properties.</p>
<p>Since Qt 4.4, <i>Qt Designer</i>'s widget box allows for a complete UI file to describe <b>one</b> custom widget. The UI file can be loaded using the <tt>&lt;ui&gt;</tt> tag. Specifying the &lt;ui&gt; tag allows for adding the &lt;customwidget&gt; element that contains additional information for custom widgets. The <tt>&lt;widget&gt;</tt> tag is sufficient if no additional information is required</p>
<p>If the custom widget does not provide a reasonable size hint, it is necessary to specify a default geometry in the string returned by the <tt>domXml()</tt> function in your subclass. For example, the <tt>AnalogClockPlugin</tt> provided by the <a href="qtdesigner-customwidgetplugin-example.html">Custom Widget Plugin</a> example, defines a default widgetgeometry in the following way:</p>
<pre class="qml">    ...
           <span class="string">&quot;  &lt;property name=\&quot;geometry\&quot;&gt;\n&quot;</span>
           <span class="string">&quot;   &lt;rect&gt;\n&quot;</span>
           <span class="string">&quot;    &lt;x&gt;0&lt;/x&gt;\n&quot;</span>
           <span class="string">&quot;    &lt;y&gt;0&lt;/y&gt;\n&quot;</span>
           <span class="string">&quot;    &lt;width&gt;100&lt;/width&gt;\n&quot;</span>
           <span class="string">&quot;    &lt;height&gt;100&lt;/height&gt;\n&quot;</span>
           <span class="string">&quot;   &lt;/rect&gt;\n&quot;</span>
           <span class="string">&quot;  &lt;/property&gt;\n&quot;</span>
    ...</pre>
<p>An additional feature of the <tt>domXml()</tt> function is that, if it returns an empty string, the widget will not be installed in <i>Qt Designer</i>'s widget box. However, it can still be used by other widgets in the form. This feature is used to hide widgets that should not be explicitly created by the user, but are required by other widgets.</p>
<p>A complete custom widget specification looks like:</p>
<pre class="cpp"><span class="operator">&lt;</span>ui language<span class="operator">=</span><span class="string">&quot;c++&quot;</span><span class="operator">&gt;</span> displayname<span class="operator">=</span><span class="string">&quot;MyWidget&quot;</span><span class="operator">&gt;</span>
    <span class="operator">&lt;</span>widget <span class="keyword">class</span><span class="operator">=</span><span class="string">&quot;widgets::MyWidget&quot;</span> name<span class="operator">=</span><span class="string">&quot;mywidget&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
    <span class="operator">&lt;</span>customwidgets<span class="operator">&gt;</span>
        <span class="operator">&lt;</span>customwidget<span class="operator">&gt;</span>
            <span class="operator">&lt;</span><span class="keyword">class</span><span class="operator">&gt;</span>widgets<span class="operator">::</span>MyWidget<span class="operator">&lt;</span><span class="operator">/</span><span class="keyword">class</span><span class="operator">&gt;</span>
            <span class="operator">&lt;</span>addpagemethod<span class="operator">&gt;</span>addPage<span class="operator">&lt;</span><span class="operator">/</span>addpagemethod<span class="operator">&gt;</span>
            <span class="operator">&lt;</span>propertyspecifications<span class="operator">&gt;</span>
                <span class="operator">&lt;</span>stringpropertyspecification name<span class="operator">=</span><span class="string">&quot;fileName&quot;</span> notr<span class="operator">=</span><span class="string">&quot;true&quot;</span> type<span class="operator">=</span><span class="string">&quot;singleline&quot;</span>
                <span class="operator">&lt;</span>stringpropertyspecification name<span class="operator">=</span><span class="string">&quot;text&quot;</span> type<span class="operator">=</span><span class="string">&quot;richtext&quot;</span>
            <span class="operator">&lt;</span><span class="operator">/</span>propertyspecifications<span class="operator">&gt;</span>
        <span class="operator">&lt;</span><span class="operator">/</span>customwidget<span class="operator">&gt;</span>
    <span class="operator">&lt;</span><span class="operator">/</span>customwidgets<span class="operator">&gt;</span>
<span class="operator">&lt;</span><span class="operator">/</span>ui<span class="operator">&gt;</span></pre>
<p>Attributes of the <tt>&lt;ui&gt;</tt> tag:</p>
<table class="generic">
 <thead><tr class="qt-style"><th >Attribute</th><th >Presence</th><th >Values</th><th >Comment</th></tr></thead>
<tr valign="top" class="odd"><td ><tt>language</tt></td><td >optional</td><td >&quot;c++&quot;, &quot;jambi&quot;</td><td >This attribute specifies the language the custom widget is intended for. It is mainly there to prevent C++-plugins from appearing in Qt Jambi.</td></tr>
<tr valign="top" class="even"><td ><tt>displayname</tt></td><td >optional</td><td >Class name</td><td >The value of the attribute appears in the Widget box and can be used to strip away namespaces.</td></tr>
</table>
<p>The <tt>&lt;addpagemethod&gt;</tt> tag tells <i>Qt Designer</i> and uic which method should be used to add pages to a container widget. This applies to container widgets that require calling a particular method to add a child rather than adding the child by passing the parent. In particular, this is relevant for containers that are not a a subclass of the containers provided in <i>Qt Designer</i>, but are based on the notion of <i>Current Page</i>. In addition, you need to provide a container extension for them.</p>
<p>The <tt>&lt;propertyspecifications&gt;</tt> element can contain a list of property meta information. Currently, properties of type string are supported. For these properties, the <tt>&lt;stringpropertyspecification&gt;</tt> tag can be used. This tag has the following attributes:</p>
<table class="generic">
 <thead><tr class="qt-style"><th >Attribute</th><th >Presence</th><th >Values</th><th >Comment</th></tr></thead>
<tr valign="top" class="odd"><td ><tt>name</tt></td><td >required</td><td >Name of the property</td></tr>
<tr valign="top" class="even"><td ><tt>type</tt></td><td >required</td><td >See below table</td><td >The value of the attribute determines how the property editor will handle them.</td></tr>
<tr valign="top" class="odd"><td ><tt>notr</tt></td><td >optional</td><td >&quot;true&quot;, &quot;false&quot;</td><td >If the attribute is &quot;true&quot;, the value is not meant to be translated.</td></tr>
</table>
<p>Values of the <tt>type</tt> attribute of the string property:</p>
<table class="generic">
 <thead><tr class="qt-style"><th >Value</th><th >Type</th></tr></thead>
<tr valign="top" class="odd"><td ><tt>&quot;richtext&quot;</tt></td><td >Rich text.</td></tr>
<tr valign="top" class="even"><td ><tt>&quot;multiline&quot;</tt></td><td >Multi-line plain text.</td></tr>
<tr valign="top" class="odd"><td ><tt>&quot;singleline&quot;</tt></td><td >Single-line plain text.</td></tr>
<tr valign="top" class="even"><td ><tt>&quot;stylesheet&quot;</tt></td><td >A CSS-style sheet.</td></tr>
<tr valign="top" class="odd"><td ><tt>&quot;objectname&quot;</tt></td><td >An object name (restricted set of valid characters).</td></tr>
<tr valign="top" class="even"><td ><tt>&quot;url&quot;</tt></td><td >URL, file name.</td></tr>
</table>
<a name="plugin-requirements"></a>
<h2>Plugin Requirements</h2>
<p>In order for plugins to work correctly on all platforms, you need to ensure that they export the symbols needed by <i>Qt Designer</i>.</p>
<p>First of all, the plugin class must be exported in order for the plugin to be loaded by <i>Qt Designer</i>. Use the <a href="../qtcore/qtplugin.html#Q_PLUGIN_METADATA">Q_PLUGIN_METADATA</a>() macro to do this. Also, the <a href="qdesignercustomwidgetinterface.html#QDESIGNER_WIDGET_EXPORT">QDESIGNER_WIDGET_EXPORT</a> macro must be used to define each custom widget class within a plugin, that <i>Qt Designer</i> will instantiate.</p>
<a name="creating-well-behaved-widgets"></a>
<h2>Creating Well Behaved Widgets</h2>
<p>Some custom widgets have special user interface features that may make them behave differently to many of the standard widgets found in <i>Qt Designer</i>. Specifically, if a custom widget grabs the keyboard as a result of a call to <a href="../qtwidgets/qwidget.html#grabKeyboard">QWidget::grabKeyboard</a>(), the operation of <i>Qt Designer</i> will be affected.</p>
<p>To give custom widgets special behavior in <i>Qt Designer</i>, provide an implementation of the initialize() function to configure the widget construction process for <i>Qt Designer</i> specific behavior. This function will be called for the first time before any calls to createWidget() and could perhaps set an internal flag that can be tested later when <i>Qt Designer</i> calls the plugin's createWidget() function.</p>
<a name="buildingandinstallingtheplugin"></a><a name="building-and-installing-the-plugin"></a>
<h2>Building and Installing the Plugin</h2>
<a name="a-simple-plugin"></a>
<h3>A Simple Plugin</h3>
<p>The <a href="qtdesigner-customwidgetplugin-example.html">Custom Widget Plugin Example</a> demonstrates a simple <i>Qt Designer</i> plugin.</p>
<p>The <tt>.pro</tt> file for a plugin must specify the headers and sources for both the custom widget and the plugin interface. Typically, this file only has to specify that the plugin's project is to be built as a library, but with specific plugin support for <i>Qt Designer</i>. This is done with the following declarations:</p>
<pre class="cpp">HEADERS     = analogclock.h \
              customwidgetplugin.h
SOURCES     = analogclock.cpp \
              customwidgetplugin.cpp
OTHER_FILES += analogclock.json</pre>
<p>If 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. To do this, include the following declaration in the plugin's <tt>.pro</tt> file:</p>
<pre class="cpp">CONFIG += release</pre>
<p>If plugins are built in a mode that is incompatible with <i>Qt Designer</i>, they will not be loaded and installed. For more information about plugins, see the <a href="../qtcore/plugins-howto.html">Plugins HOWTO</a> document.</p>
<p>It is also necessary to ensure that the plugin is installed together with other <i>Qt Designer</i> widget plugins:</p>
<pre class="cpp">target.path = $$[QT_INSTALL_PLUGINS]/designer
INSTALLS += target</pre>
<p>The <tt>$[QT_INSTALL_PLUGINS]</tt> variable is a placeholder to the location of the installed Qt plugins. You can configure <i>Qt Designer</i> to look for plugins in other locations by setting the <tt>QT_PLUGIN_PATH</tt> environment variable before running the application.</p>
<p><b>Note: </b><i>Qt Designer</i> will look for a <tt>designer</tt> subdirectory in each path supplied.</p><p>See <a href="../qtcore/qcoreapplication.html#libraryPaths">QCoreApplication::libraryPaths</a>() for more information about customizing paths for libraries and plugins with Qt applications.</p>
<a name="splitting-up-the-plugin"></a>
<h3>Splitting up the Plugin</h3>
<p>In a real world scenario, you do not want to have dependencies of the application making use of the custom widgets to the <i>Qt Designer</i> headers and libraries as introduced by the simple approach explained above.</p>
<p>There are two ways to resolve this:</p>
<ul>
<li>Create a <tt>.pri</tt> file that contains the headers sources and sources of the custom widget:<pre class="cpp">           INCLUDEPATH <span class="operator">+</span><span class="operator">=</span> $$PWD
           HEADERS <span class="operator">+</span><span class="operator">=</span> $$PWD<span class="operator">/</span>analogclock<span class="operator">.</span>h
           SOURCES <span class="operator">+</span><span class="operator">=</span> $$PWD<span class="operator">/</span>analogclock<span class="operator">.</span>cpp</pre>
<p>This file would then be included by the <tt>.pro</tt> file of the plugin and the application:</p>
<pre class="cpp">           <span class="keyword">include</span>(customwidget<span class="operator">.</span>pri)</pre>
<p>Running <tt>qmake -Wall</tt> on the <tt>.pro</tt> files causes a warning to be printed if an included <tt>.pri</tt> file cannot be found.</p>
</li>
<li>Create a standalone shared library containing the custom widgets only as described in Creating Shared Libraries.<p>This library would then be used by the application as well as by the <i>Qt Designer</i> plugin. Care must be taken to ensure that the plugin can locate the library at run-time.</p>
</li>
</ul>
<a name="related-examples"></a>
<h2>Related Examples</h2>
<p>For more information on using custom widgets in <i>Qt Designer</i>, refer to the <a href="qtdesigner-customwidgetplugin-example.html">Custom Widget Plugin</a> and <a href="qtdesigner-worldtimeclockplugin-example.html">World Time Clock Plugin</a> examples for more information about using custom widgets in <i>Qt Designer</i>. Also, you can use the <a href="qdesignercustomwidgetcollectioninterface.html">QDesignerCustomWidgetCollectionInterface</a> class to combine several custom widgets into a single library.</p>
</div>
<!-- @@@designer-creating-custom-widgets.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="designer-using-custom-widgets.html">Using Custom Widgets with Qt Designer</a>
<a class="nextPage" href="designer-creating-custom-widgets-extensions.html">Creating Custom Widget Extensions</a>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</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>