QDesignerCustomWidgetInterface Class
The QDesignerCustomWidgetInterface class enables Qt Designer to access and construct custom widgets. More...
Header: | #include <QDesignerCustomWidgetInterface> |
qmake: | QT += designer |
Public Functions
virtual | ~QDesignerCustomWidgetInterface() |
virtual QString | codeTemplate() const |
virtual QWidget * | createWidget(QWidget *parent) = 0 |
virtual QString | domXml() const |
virtual QString | group() const = 0 |
virtual QIcon | icon() const = 0 |
virtual QString | includeFile() const = 0 |
virtual void | initialize(QDesignerFormEditorInterface *core) |
virtual bool | isContainer() const = 0 |
virtual bool | isInitialized() const |
virtual QString | name() const = 0 |
virtual QString | toolTip() const = 0 |
virtual QString | whatsThis() const = 0 |
Macros
Detailed Description
The QDesignerCustomWidgetInterface class enables Qt Designer to access and construct custom widgets.
QDesignerCustomWidgetInterface provides a custom widget with an interface. The class contains a set of functions that must be subclassed to return basic information about the widget, such as its class name and the name of its header file. Other functions must be implemented to initialize the plugin when it is loaded, and to construct instances of the custom widget for Qt Designer to use.
When implementing a custom widget you must subclass QDesignerCustomWidgetInterface to expose your widget to Qt Designer. For example, this is the declaration for the plugin used in the Custom Widget Plugin example that enables an analog clock custom widget to be used by Qt Designer:
class AnalogClockPlugin : public QObject, public QDesignerCustomWidgetInterface { Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface") Q_INTERFACES(QDesignerCustomWidgetInterface) public: explicit AnalogClockPlugin(QObject *parent = nullptr); bool isContainer() const override; bool isInitialized() const override; QIcon icon() const override; QString domXml() const override; QString group() const override; QString includeFile() const override; QString name() const override; QString toolTip() const override; QString whatsThis() const override; QWidget *createWidget(QWidget *parent) override; void initialize(QDesignerFormEditorInterface *core) override; private: bool initialized = false; };
Note that the only part of the class definition that is specific to this particular custom widget is the class name. In addition, since we are implementing an interface, we must ensure that it's made known to the meta object system using the Q_INTERFACES() macro. This enables Qt Designer to use the qobject_cast() function to query for supported interfaces using nothing but a QObject pointer.
After Qt Designer loads a custom widget plugin, it calls the interface's initialize() function to enable it to set up any resources that it may need. This function is called with a QDesignerFormEditorInterface parameter that provides the plugin with a gateway to all of Qt Designer's API.
Qt Designer constructs instances of the custom widget by calling the plugin's createWidget() function with a suitable parent widget. Plugins must construct and return an instance of a custom widget with the specified parent widget.
Exporting your custom widget plugin to Qt Designer using the Q_PLUGIN_METADATA() macro. For example, if a library called libcustomwidgetplugin.so
(on Unix) or libcustomwidget.dll
(on Windows) contains a widget class called MyCustomWidget
, we can export it by adding the following line to the file containing the plugin header:
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface")
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 it.
When implementing a custom widget plugin, you build it as a separate library. If you want to include several custom widget plugins in the same library, you must in addition subclass QDesignerCustomWidgetCollectionInterface.
Warning: If your custom widget plugin contains QVariant properties, be aware that only the following types are supported:
- QVariant::ByteArray
- QVariant::Bool
- QVariant::Color
- QVariant::Cursor
- QVariant::Date
- QVariant::DateTime
- QVariant::Double
- QVariant::Int
- QVariant::Point
- QVariant::Rect
- QVariant::Size
- QVariant::SizePolicy
- QVariant::String
- QVariant::Time
- QVariant::UInt
For a complete example using the QDesignerCustomWidgetInterface class, see the Custom Widget Example. The example shows how to create a custom widget plugin for Qt Designer.
See also QDesignerCustomWidgetCollectionInterface and Creating Custom Widgets for Qt Designer.
Member Function Documentation
[virtual]
QDesignerCustomWidgetInterface::~QDesignerCustomWidgetInterface()
Destroys the instance of QDesignerCustomWidgetInterface. The destructor is virtual.
[virtual]
QString QDesignerCustomWidgetInterface::codeTemplate() const
[pure virtual]
QWidget *QDesignerCustomWidgetInterface::createWidget(QWidget *parent)
[virtual]
QString QDesignerCustomWidgetInterface::domXml() const
[pure virtual]
QString QDesignerCustomWidgetInterface::group() const
[pure virtual]
QIcon QDesignerCustomWidgetInterface::icon() const
[pure virtual]
QString QDesignerCustomWidgetInterface::includeFile() const
[virtual]
void QDesignerCustomWidgetInterface::initialize(QDesignerFormEditorInterface *core)
[pure virtual]
bool QDesignerCustomWidgetInterface::isContainer() const
[virtual]
bool QDesignerCustomWidgetInterface::isInitialized() const
[pure virtual]
QString QDesignerCustomWidgetInterface::name() const
[pure virtual]
QString QDesignerCustomWidgetInterface::toolTip() const
[pure virtual]
QString QDesignerCustomWidgetInterface::whatsThis() const
Macro Documentation
QDESIGNER_WIDGET_EXPORT
This macro is used when defining custom widgets to ensure that they are correctly exported from plugins for use with Qt Designer.
On some platforms, the symbols required by Qt Designer to create new widgets are removed from plugins by the build system, making them unusable. Using this macro ensures that the symbols are retained on those platforms, and has no side effects on other platforms.
For example, the World Time Clock Plugin example exports a custom widget class with the following declaration:
class QDESIGNER_WIDGET_EXPORT WorldTimeClock : public QWidget { Q_OBJECT ... };
This function was introduced in Qt 4.1.
See also Creating Custom Widgets for Qt Designer.