QtWidget - creating an extended pushbutton plugin
-
Hello,
I want to create a pushbutton plugin which I can copy to [QtCreatorsDir]/bin/plugins/designer and use in QtCreator as a new kind of button.
For that I created a class named "XPushButton" wihch extends QPushButton and a new widget plugin using the form provided by QtCreator.
XPushButtonPlugin.h
@#ifndef XPUSHBUTTONPLUGIN_H
#define XPUSHBUTTONPLUGIN_H#include <QDesignerCustomWidgetInterface>
class XPushButtonPlugin : public QObject, public QDesignerCustomWidgetInterface
{
Q_OBJECT
Q_INTERFACES(QDesignerCustomWidgetInterface)
#if QT_VERSION >= 0x050000
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface")
#endif // QT_VERSION >= 0x050000public:
XPushButtonPlugin(QObject *parent = 0);bool isContainer() const; bool isInitialized() const; QIcon icon() const; QString domXml() const; QString group() const; QString includeFile() const; QString name() const; QString toolTip() const; QString whatsThis() const; QWidget *createWidget(QWidget *parent); void initialize(QDesignerFormEditorInterface *core);
private:
bool m_initialized;
};#endif
@XPushButtonPlugin.cpp
@#include "XPushButton.h"
#include "XPushButtonPlugin.h"#include <QtPlugin>
XPushButtonPlugin::XPushButtonPlugin(QObject *parent)
: QObject(parent)
{
m_initialized = false;
}void XPushButtonPlugin::initialize(QDesignerFormEditorInterface * /* core */)
{
if (m_initialized)
return;// Add extension registrations, etc. here m_initialized = true;
}
bool XPushButtonPlugin::isInitialized() const
{
return m_initialized;
}QWidget *XPushButtonPlugin::createWidget(QWidget *parent)
{
return new XPushButton(parent);
}QString XPushButtonPlugin::name() const
{
return QLatin1String("XPushButton");
}QString XPushButtonPlugin::group() const
{
return QLatin1String("NEW BUTTONS");
}QIcon XPushButtonPlugin::icon() const
{
return QIcon();
}QString XPushButtonPlugin::toolTip() const
{
return QLatin1String("");
}QString XPushButtonPlugin::whatsThis() const
{
return QLatin1String("");
}bool XPushButtonPlugin::isContainer() const
{
return false;
}QString XPushButtonPlugin::domXml() const
{
return QLatin1String("<widget class="XPushButton" name="xPushButton">\n</widget>\n");
}QString XPushButtonPlugin::includeFile() const
{
return QLatin1String("XPushButton.h");
}
#if QT_VERSION < 0x050000
Q_EXPORT_PLUGIN2(XPushButtonPlugin, XPushButtonPlugin)
#endif // QT_VERSION < 0x050000
@The definitions of the new buttons are in the XPushButton.h file in a format of
@
Q_PROPERTY(QString eventName READ getEventName WRITE setEventName NOTIFY eventNameChanged)
Q_PROPERTY(QString eventValue READ getEventValue WRITE setEventValue NOTIFY eventValueChanged)
@But after compiling and copying it into the Qt plugindesigner dir and restarting QtCreator there is no new button in the QtDesigner.
I think this is because I don't define something in the XPushButtonPlugin::initialize function.
My second problem is, that I don't know how to extend the propertiessheet by dynamic properties. I thought that this would be possible with the created plugin and I already read articles to this theme on the Internet but I dont get it. Can anyone explain it to me or give me some informationsites for Qt 5.2?
Thank you,
-
Hello,
I successfully created and installed some custom widgets plugins in Qt Creator in the past.
bq. But after compiling and copying it into the Qt plugindesigner dir and restarting QtCreator there is no new button in the QtDesigner.
In Qt creator designer select Tools->Form editor->About Qt designer plugins... it should give you some information on why your plugins are not loaded.
Please notice that Qt Creator can only load plugins compiled by the same compiler used to build Qt Creator itself (you can see that information on Help->About Qt Creator)bq. I think this is because I don’t define something in the XPushButtonPlugin::initialize function.
No, that is not the problem. If you don't need any extra functionality yourself, then the automatically generated code is enough.
bq. My second problem is, that I don’t know how to extend the propertiessheet by dynamic properties.
I don't know what you mean by dynamic properties. The two new extra properties you defined should appear automatically in designer's property editor - no extra steps required.
Hope it helps,
H.