/doc/src/examples/customwidgetplugin.qdoc

https://bitbucket.org/ultra_iter/qt-vtl
Unknown | 238 lines | 175 code | 63 blank | 0 comment | 0 complexity | 6149121541461c9e581b26b6395a0630 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 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. ** GNU Free Documentation License
  11. ** Alternatively, this file may be used under the terms of the GNU Free
  12. ** Documentation License version 1.3 as published by the Free Software
  13. ** Foundation and appearing in the file included in the packaging of
  14. ** this file.
  15. **
  16. ** Other Usage
  17. ** Alternatively, this file may be used in accordance with the terms
  18. ** and conditions contained in a signed written agreement between you
  19. ** and Nokia.
  20. **
  21. **
  22. **
  23. **
  24. ** $QT_END_LICENSE$
  25. **
  26. ****************************************************************************/
  27. /*!
  28. \example designer/customwidgetplugin
  29. \title Custom Widget Plugin Example
  30. \brief The Custom Widget example shows how to create a custom widget plugin for \QD.
  31. \image customwidgetplugin-example.png
  32. In this example, the custom widget used is based on the
  33. \l{widgets/analogclock}{Analog Clock example}, and does not provide any custom
  34. signals or slots.
  35. \section1 Preparation
  36. To provide a custom widget that can be used with \QD, we need to supply a
  37. self-contained implementation and provide a plugin interface. In this
  38. example, we reuse the \l{widgets/analogclock}{Analog Clock example} for
  39. convenience.
  40. Since custom widgets plugins rely on components supplied with \QD, the
  41. project file that we use needs to contain information about \QD's
  42. library components:
  43. \snippet examples/designer/customwidgetplugin/customwidgetplugin.pro 2
  44. \snippet examples/designer/customwidgetplugin/customwidgetplugin.pro 0
  45. The \c TEMPLATE variable's value makes \c qmake create the custom
  46. widget as a library. Later, we will ensure that the widget will be
  47. recognized as a plugin by Qt by using the Q_EXPORT_PLUGIN2() macro
  48. to export the relevant widget information.
  49. The \c CONFIG variable contains two values, \c designer and \c
  50. plugin:
  51. \list
  52. \o \c designer: Since custom widgets plugins rely on components
  53. supplied with \QD, this value ensures that our plugin links
  54. against \QD's library (\c libQtDesigner.so).
  55. \o \c plugin: We also need to ensure that \c qmake considers the
  56. custom widget a plugin library.
  57. \endlist
  58. When Qt is configured to build in both debug and release modes,
  59. \QD will be built in release mode. When this occurs, it is
  60. necessary to ensure that plugins are also built in release
  61. mode. For that reason we add the \c debug_and_release value to the
  62. \c CONFIG variable. Otherwise, if a plugin is built in a mode that
  63. is incompatible with \QD, it won't be loaded and
  64. installed.
  65. The header and source files for the widget are declared in the usual way,
  66. and we provide an implementation of the plugin interface so that \QD can
  67. use the custom widget:
  68. \snippet examples/designer/customwidgetplugin/customwidgetplugin.pro 3
  69. It is also important to ensure that the plugin is installed in a
  70. location that is searched by \QD. We do this by specifying a
  71. target path for the project and adding it to the list of items to
  72. install:
  73. \snippet doc/src/snippets/code/doc_src_examples_customwidgetplugin.pro 0
  74. The custom widget is created as a library, and will be installed
  75. alongside the other \QD plugins when the project is installed
  76. (using \c{make install} or an equivalent installation procedure).
  77. Later, we will ensure that it is recognized as a plugin by \QD by
  78. using the Q_EXPORT_PLUGIN2() macro to export the relevant widget
  79. information.
  80. Note that if you want the plugins to appear in a Visual Studio
  81. integration, the plugins must be built in release mode and their
  82. libraries must be copied into the plugin directory in the install
  83. path of the integration (for an example, see \c {C:/program
  84. files/trolltech as/visual studio integration/plugins}).
  85. For more information about plugins, see the \l {How to
  86. Create Qt Plugins} documentation.
  87. \section1 AnalogClock Class Definition and Implementation
  88. The \c AnalogClock class is defined and implemented in exactly the same
  89. way as described in the \l{widgets/analogclock}{Analog Clock example}.
  90. Since the class is self-contained, and does not require any external
  91. configuration, it can be used without modification as a custom widget in
  92. \QD.
  93. \section1 AnalogClockPlugin Class Definition
  94. The \c AnalogClock class is exposed to \QD through the \c
  95. AnalogClockPlugin class. This class inherits from both QObject and
  96. the QDesignerCustomWidgetInterface class, and implements an
  97. interface defined by QDesignerCustomWidgetInterface:
  98. \snippet examples/designer/customwidgetplugin/customwidgetplugin.h 0
  99. The functions provide information about the widget that \QD can use in
  100. the \l{Getting to Know Qt Designer#WidgetBox}{widget box}.
  101. The \c initialized private member variable is used to record whether
  102. the plugin has been initialized by \QD.
  103. Note that the only part of the class definition that is specific to
  104. this particular custom widget is the class name.
  105. \section1 AnalogClockPlugin Implementation
  106. The class constructor simply calls the QObject base class constructor
  107. and sets the \c initialized variable to \c false.
  108. \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 0
  109. \QD will initialize the plugin when it is required by calling the
  110. \c initialize() function:
  111. \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 1
  112. In this example, the \c initialized private variable is tested, and only
  113. set to \c true if the plugin is not already initialized. Although, this
  114. plugin does not require any special code to be executed when it is
  115. initialized, we could include such code after the test for initialization.
  116. The \c isInitialized() function lets \QD know whether the plugin is
  117. ready for use:
  118. \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 2
  119. Instances of the custom widget are supplied by the \c createWidget()
  120. function. The implementation for the analog clock is straightforward:
  121. \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 3
  122. In this case, the custom widget only requires a \c parent to be specified.
  123. If other arguments need to be supplied to the widget, they can be
  124. introduced here.
  125. The following functions provide information for \QD to use to represent
  126. the widget in the widget box.
  127. The \c name() function returns the name of class that provides the
  128. custom widget:
  129. \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 4
  130. The \c group() function is used to describe the type of widget that the
  131. custom widget belongs to:
  132. \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 5
  133. The widget plugin will be placed in a section identified by its
  134. group name in \QD's widget box. The icon used to represent the
  135. widget in the widget box is returned by the \c icon() function:
  136. \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 6
  137. In this case, we return a null icon to indicate that we have no icon
  138. that can be used to represent the widget.
  139. A tool tip and "What's This?" help can be supplied for the custom widget's
  140. entry in the widget box. The \c toolTip() function should return a short
  141. message describing the widget:
  142. \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 7
  143. The \c whatsThis() function can return a longer description:
  144. \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 8
  145. The \c isContainer() function tells \QD whether the widget is supposed to
  146. be used as a container for other widgets. If not, \QD will not allow the
  147. user to place widgets inside it.
  148. \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 9
  149. Most widgets in Qt can contain child widgets, but it only makes sense
  150. to use dedicated container widgets for this purpose in \QD. By returning
  151. \c false, we indicate that the custom widget cannot hold other widgets;
  152. if we returned true, \QD would allow other widgets to be placed inside
  153. the analog clock and a layout to be defined.
  154. The \c domXml() function provides a way to include default settings for
  155. the widget in the standard XML format used by \QD. In this case, we only
  156. specify the widget's geometry:
  157. \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 10
  158. If the widget provides a reasonable size hint, it is not necessary to
  159. define it here. In addition, returning an empty string instead of a
  160. \c{<widget>} element will tell \QD not to install the widget in the
  161. widget box.
  162. To make the analog clock widget usable by applications, we implement
  163. the \c includeFile() function to return the name of the header file
  164. containing the custom widget class definition:
  165. \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 12
  166. Finally, we use the Q_EXPORT_PLUGIN2() macro to export the \c
  167. AnalogClockPlugin class for use with \QD:
  168. \snippet examples/designer/customwidgetplugin/customwidgetplugin.cpp 13
  169. This macro ensures that \QD can access and construct the custom widget.
  170. Without this macro, there is no way for \QD to use the widget.
  171. It is important to note that you can only use the Q_EXPORT_PLUGIN2()
  172. macro once in any implementation. If you have several custom widgets in
  173. an implementation that you wish to make available to \QD, you will need
  174. to implement \l{QDesignerCustomWidgetCollectionInterface}.
  175. */