![]() |
Home · All Classes · Main Classes · Annotated · Grouped Classes · Functions | ![]() |
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.
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:
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.
The custom widget is created as a library.
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.
The AnalogClock class is exposed to Qt Designer through the AnalogClockPlugin class. This class inherits from both QObject and the ICustomWidget class, and implements an interface defined by ICustomWidget:
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.
The class constructor simply calls the QObject base class constructor and sets the initialized variable to false.
Qt Designer will initialize the plugin when it is required by calling the initialize() function:
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:
Instances of the custom widget are supplied by the createWidget() function. The implementation for the analog clock is straightforward:
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:
The group() function is used to describe the type of widget that the custom widget belongs to:
Note that all widget plugins will be placed in the "Custom Widgets" section of Qt Designer's widget box. The icon used to represent the widget in the widget box is returned by the icon() function:
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:
The whatsThis() function can return a longer description:
The isContainer() and isForm() functions provide important information about the way that the widget is to be handled by Qt Designer.
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.
In this example, we return false for this function because we want the analog clock widget to be used with other widgets on a form. However, it is also possible to create custom widgets that can be used as forms in their own right.
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:
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:
We can provide a sample code template for the custom widget with the codeTemplate() function:
The code template is optionally used to specify how the widget is used in application code.
Finally, we use the Q_EXPORT_PLUGIN macro to export the AnalogClockPlugin class for use with Qt Designer:
Copyright © 2005 Trolltech | Trademarks | Qt 4.0.0-b2 |