Custom Widget Plugin Example

Files:

The Custom Widget example shows how to create a custom widget plugin for Qt Designer.

In this example, the custom widget used is based on the Analog Clock example, and does not provide any custom signals or slots.

Preparation

To provide a custom widget that can be used with Qt Designer, we need to supply a self-contained implementation and provide a plugin interface. In this example, we reuse the Analog Clock example for convenience.

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

  1. TEMPLATE    = lib
  2. CONFIG      += designer plugin

The TEMPLATE variable's value makes 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_EXPORT_PLUGIN2() macro to export the relevant widget information.

The CONFIG variable contains two values, designer and plugin:

  • designer: Since custom widgets plugins rely on components supplied with Qt Designer, this value ensures that our plugin links against Qt Designer's library (libQtDesigner.so).
  • plugin: We also need to ensure that qmake considers the custom widget a plugin library.

When Qt is configured to build in both debug and release modes, Qt Designer 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 debug_and_release value to the CONFIG variable. Otherwise, if a plugin is built in a mode that is incompatible with Qt Designer, it won't be loaded and installed.

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 Qt Designer can use the custom widget:

  1. HEADERS     = analogclock.h \
  2.               customwidgetplugin.h
  3. SOURCES     = analogclock.cpp \
  4.               customwidgetplugin.cpp

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

  1. target.path = $$[QT_INSTALL_PLUGINS]/designer
  2. INSTALLS += target

The custom widget is created as a library, and will be installed alongside the other Qt Designer plugins when the project is installed (using make install or an equivalent installation procedure). Later, we will ensure that it is recognized as a plugin by Qt Designer by using the Q_EXPORT_PLUGIN2() 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:/program files/trolltech as/visual studio integration/plugins).

For more information about plugins, see the How to Create Qt Plugins documentation.

AnalogClock Class Definition and Implementation

The AnalogClock class is defined and implemented in exactly the same way as described in the 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 Qt Designer.

AnalogClockPlugin Class Definition

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

  1. class AnalogClockPlugin : public QObject, public QDesignerCustomWidgetInterface
  2.  {
  3.     Q_OBJECT
  4.     Q_INTERFACES(QDesignerCustomWidgetInterface)
  5.  
  6. public:
  7.     AnalogClockPlugin(QObject *parent = 0);
  8.  
  9.     bool isContainer() const;
  10.     bool isInitialized() const;
  11.     QIcon icon() const;
  12.     QString domXml() const;
  13.     QString group() const;
  14.     QString includeFile() const;
  15.     QString name() const;
  16.     QString toolTip() const;
  17.     QString whatsThis() const;
  18.     QWidget *createWidget(QWidget *parent);
  19.     void initialize(QDesignerFormEditorInterface *core);
  20.  
  21. private:
  22.     bool initialized;
  23. };

The functions provide information about the widget that Qt Designer can use in the widget box. The initialized private member variable is used to record whether the plugin has been initialized by Qt Designer.

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

AnalogClockPlugin Implementation

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

  1. AnalogClockPlugin::AnalogClockPlugin(QObject *parent)
  2.     : QObject(parent)
  3.  {
  4.     initialized = false;
  5. }

Qt Designer will initialize the plugin when it is required by calling the initialize() function:

  1. void AnalogClockPlugin::initialize(QDesignerFormEditorInterface * /* core */)
  2.  {
  3.     if (initialized)
  4.         return;
  5.  
  6.     initialized = true;
  7. }

In this example, the initialized private variable is tested, and only set to 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 isInitialized() function lets Qt Designer know whether the plugin is ready for use:

  1. bool AnalogClockPlugin::isInitialized() const
  2.  {
  3.     return initialized;
  4. }

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

  1.                 QWidget *AnalogClockPlugin::createWidget(QWidget *parent)
  2.  {
  3.     return new AnalogClock(parent);
  4. }

In this case, the custom widget only requires a 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 Qt Designer to use to represent the widget in the widget box. The name() function returns the name of class that provides the custom widget:

  1.                 QString AnalogClockPlugin::name() const
  2.  {
  3.     return "AnalogClock";
  4. }

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

  1.                 QString AnalogClockPlugin::group() const
  2.  {
  3.     return "Display Widgets [Examples]";
  4. }

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

  1.                 QIcon AnalogClockPlugin::icon() const
  2.  {
  3.     return QIcon();
  4. }

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 toolTip() function should return a short message describing the widget:

  1.                 QString AnalogClockPlugin::toolTip() const
  2.  {
  3.     return "";
  4. }

The whatsThis() function can return a longer description:

  1.                 QString AnalogClockPlugin::whatsThis() const
  2.  {
  3.     return "";
  4. }

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

  1. bool AnalogClockPlugin::isContainer() const
  2.  {
  3.     return false;
  4. }

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

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

  1.                 QString AnalogClockPlugin::domXml() const
  2.  {
  3.     return "<ui language=\"c++\">\n"
  4.            " <widget class=\"AnalogClock\" name=\"analogClock\">\n"
  5.            "  <property name=\"geometry\">\n"
  6.            "   <rect>\n"
  7.            "    <x>0</x>\n"
  8.            "    <y>0</y>\n"
  9.            "    <width>100</width>\n"
  10.            "    <height>100</height>\n"
  11.            "   </rect>\n"
  12.            "  </property>\n"
  13.            "  <property name=\"toolTip\" >\n"
  14.            "   <string>The current time</string>\n"
  15.            "  </property>\n"
  16.            "  <property name=\"whatsThis\" >\n"
  17.            "   <string>The analog clock widget displays the current time.</string>\n"
  18.            "  </property>\n"
  19.            " </widget>\n"
  20.            "</ui>\n";
  21. }

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 <widget> element will tell Qt Designer not to install the widget in the widget box.

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

  1.                 QString AnalogClockPlugin::includeFile() const
  2.  {
  3.     return "analogclock.h";
  4. }

Finally, we use the Q_EXPORT_PLUGIN2() macro to export the AnalogClockPlugin class for use with Qt Designer:

  1. Q_EXPORT_PLUGIN2(customwidgetplugin, AnalogClockPlugin)

This macro ensures that Qt Designer can access and construct the custom widget. Without this macro, there is no way for Qt Designer to use the widget.

It is important to note that you can only use the Q_EXPORT_PLUGIN2() macro once in any implementation. If you have several custom widgets in an implementation that you wish to make available to Qt Designer, you will need to implement QDesignerCustomWidgetCollectionInterface.

Notes provided by the Qt Community

No notes