I created a new plugin in Qt 6.5 using Visual Studio C++ 2022 Community programming environment for Windows 10. This plugin creates the new widget QLed by P. Sereno. In this plugin you can change the shape and color of the led directly in Qt Designer (created for Visual Studio C++). The plugin would work, more precisely in changing the color, but it doesn't work in changing the shape, i.e. the shape chosen in Qt Designer is not maintained when I use this plugin in Qt Creator.
I can see this right away when in Qt Designer I create the form for the test application, where I choose a widget shape (which I see listed in the plugin panel) but that doesn't change the plugin shape on the form I'm building (see attached figure).
Below I put the qled.h file of the widget:
#pragma once
#include <Qt>
#include <QtWidgets/QWidget>
#include <QtUiPlugin/QDesignerExportWidget>
// My Qt designer widget plugin class
class QDESIGNER_WIDGET_EXPORT QLed
: public QWidget{
Q_OBJECT
Q_ENUMS (ledColor)
Q_ENUMS (ledShape)
Q_PROPERTY(bool value READ value WRITE setValue)
Q_PROPERTY(ledColor onColor READ onColor WRITE setOnColor)
Q_PROPERTY(ledColor offColor READ offColor WRITE setOffColor)
//Q_PROPERTY(ledShape shape READ shape WRITE setShape NOTIFY shapeChanged)
Q_PROPERTY(ledShape shape READ shape WRITE setShape)
public:
explicit QLed
(QWidget *parent
= 0);
bool value() const { return m_value; }
enum ledColor { Red = 0, Green, Yellow, Grey, Orange, Purple, Blue };
enum ledShape { Circle = 0, Square, Triangle, Rounded };
ledColor onColor() const { return m_onColor; }
ledColor offColor() const { return m_offColor; }
ledShape shape() const { return m_shape; }
public slots:
void setValue(bool);
void setOnColor(ledColor);
void setOffColor(ledColor);
void setShape(ledShape);
void toggleValue();
signals:
void shapeChanged(ledShape);
protected:
bool m_value;
ledColor m_onColor, m_offColor;
int id_Timer;
ledShape m_shape;
};
#pragma once
#include <Qt>
#include <QtWidgets/QWidget>
#include <QtUiPlugin/QDesignerExportWidget>
// My Qt designer widget plugin class
class QColor;
class QDESIGNER_WIDGET_EXPORT QLed : public QWidget
{
Q_OBJECT
Q_ENUMS (ledColor)
Q_ENUMS (ledShape)
Q_PROPERTY(bool value READ value WRITE setValue)
Q_PROPERTY(ledColor onColor READ onColor WRITE setOnColor)
Q_PROPERTY(ledColor offColor READ offColor WRITE setOffColor)
//Q_PROPERTY(ledShape shape READ shape WRITE setShape NOTIFY shapeChanged)
Q_PROPERTY(ledShape shape READ shape WRITE setShape)
public:
explicit QLed(QWidget *parent = 0);
bool value() const { return m_value; }
enum ledColor { Red = 0, Green, Yellow, Grey, Orange, Purple, Blue };
enum ledShape { Circle = 0, Square, Triangle, Rounded };
ledColor onColor() const { return m_onColor; }
ledColor offColor() const { return m_offColor; }
ledShape shape() const { return m_shape; }
public slots:
void setValue(bool);
void setOnColor(ledColor);
void setOffColor(ledColor);
void setShape(ledShape);
void toggleValue();
signals:
void shapeChanged(ledShape);
protected:
bool m_value;
ledColor m_onColor, m_offColor;
int id_Timer;
ledShape m_shape;
QStringList shapes;
QStringList colors;
void paintEvent(QPaintEvent *event) override;
};
To copy to clipboard, switch view to plain text mode
Below I put the qled.cpp file of the widget:
#include <QColor>
#include <QtGlobal>
#include <QtGui>
#include <QPolygon>
#include <QtSvg>
#include <QSvgRenderer>
#include "qled.h"
/*!
\brief QLed: this is the QLed constructor.
\param parent: The Parent Widget
*/
{
m_value = false;
m_onColor = Red;
m_offColor = Grey;
m_shape = Circle;
setMinimumSize
(QSize(50,
50));
shapes << ":/resources/circle_" << ":/resources/square_" << ":/resources/triang_" << ":/resources/round_";
colors << "red.svg" << "green.svg" << "yellow.svg" << "grey.svg" << "orange.svg" << "purple.svg" << "blue.svg";
}
/*!
\brief paintEvent: painting method
\param QPaintEvent *
\return void
*/
{
painter.
setRenderHint(QPainter::Antialiasing,
true);
ledShapeAndColor = shapes[m_shape];
if(m_value)
ledShapeAndColor.append(colors[m_onColor]);
else
ledShapeAndColor.append(colors[m_offColor]);
renderer->load(ledShapeAndColor);
renderer->render(&painter);
renderer = nullptr;
delete renderer;
}
/*!
\brief setOnColor: this method allows to change the On color {Red,Green,Yellow,Grey,Orange,Purple,blue}
\param ledColor newColor
\return void
*/
void QLed::setOnColor(ledColor newColor)
{
m_onColor = newColor;
update();
}
/*!
\brief setOffColor: this method allows to change the Off color {Red,Green,Yellow,Grey,Orange,Purple,blue}
\param ledColor newColor
\return void
*/
void QLed::setOffColor(ledColor newColor)
{
m_offColor = newColor;
update();
}
/*!
\brief setShape: this method allows to change the led shape {Circle,Square,Triangle,Rounded rectangle}
\param ledColor newColor
\return void
*/
void QLed::setShape(ledShape newShape)
{
m_shape = newShape;
update();
}
/*!
\brief setValue: this method allows to set the led value {true,false}
\param ledColor newColor
\return void
*/
void QLed::setValue(bool value)
{
m_value = value;
update();
}
/*!
\brief toggleValue: this method toggles the led value
\param ledColor newColor
\return void
*/
void QLed::toggleValue()
{
m_value =! m_value;
update();
}
#include <QColor>
#include <QtGlobal>
#include <QtGui>
#include <QPolygon>
#include <QtSvg>
#include <QSvgRenderer>
#include "qled.h"
/*!
\brief QLed: this is the QLed constructor.
\param parent: The Parent Widget
*/
QLed::QLed(QWidget *parent) : QWidget(parent)
{
m_value = false;
m_onColor = Red;
m_offColor = Grey;
m_shape = Circle;
setMinimumSize(QSize(50,50));
shapes << ":/resources/circle_" << ":/resources/square_" << ":/resources/triang_" << ":/resources/round_";
colors << "red.svg" << "green.svg" << "yellow.svg" << "grey.svg" << "orange.svg" << "purple.svg" << "blue.svg";
}
/*!
\brief paintEvent: painting method
\param QPaintEvent *
\return void
*/
void QLed::paintEvent(QPaintEvent *)
{
QSvgRenderer *renderer = new QSvgRenderer();
QString ledShapeAndColor;
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
ledShapeAndColor = shapes[m_shape];
if(m_value)
ledShapeAndColor.append(colors[m_onColor]);
else
ledShapeAndColor.append(colors[m_offColor]);
renderer->load(ledShapeAndColor);
renderer->render(&painter);
renderer = nullptr;
delete renderer;
}
/*!
\brief setOnColor: this method allows to change the On color {Red,Green,Yellow,Grey,Orange,Purple,blue}
\param ledColor newColor
\return void
*/
void QLed::setOnColor(ledColor newColor)
{
m_onColor = newColor;
update();
}
/*!
\brief setOffColor: this method allows to change the Off color {Red,Green,Yellow,Grey,Orange,Purple,blue}
\param ledColor newColor
\return void
*/
void QLed::setOffColor(ledColor newColor)
{
m_offColor = newColor;
update();
}
/*!
\brief setShape: this method allows to change the led shape {Circle,Square,Triangle,Rounded rectangle}
\param ledColor newColor
\return void
*/
void QLed::setShape(ledShape newShape)
{
m_shape = newShape;
update();
}
/*!
\brief setValue: this method allows to set the led value {true,false}
\param ledColor newColor
\return void
*/
void QLed::setValue(bool value)
{
m_value = value;
update();
}
/*!
\brief toggleValue: this method toggles the led value
\param ledColor newColor
\return void
*/
void QLed::toggleValue()
{
m_value =! m_value;
update();
}
To copy to clipboard, switch view to plain text mode
Below I put the qledplugin.h file of the widget:
#pragma once
#include <QDesignerCustomWidgetInterface>
// See Cusotm Widget Plugin Example in Qt online documentation to understand the meaning of this file
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface")
public:
bool isContainer() const override;
bool isInitialized() const override;
QIcon icon
() const override;
QString includeFile
() const override;
QString whatsThis
() const override;
private:
bool initialized = false;
};
#pragma once
#include <QDesignerCustomWidgetInterface>
// See Cusotm Widget Plugin Example in Qt online documentation to understand the meaning of this file
class QLedPlugin : public QObject, public QDesignerCustomWidgetInterface
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface")
Q_INTERFACES(QDesignerCustomWidgetInterface)
public:
QLedPlugin(QObject *parent = 0);
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;
};
To copy to clipboard, switch view to plain text mode
Below I put the qledplugin.cpp file of the widget:
#include "qled.h"
#include "qledplugin.h"
#include <QtPlugin>
using namespace Qt::StringLiterals;
// See Cusotm Widget Plugin Example in Qt online documentation to understand the meaning of this file
QLedPlugin
::QLedPlugin(QObject *parent
){
initialized = false;
}
{
if (initialized)
return;
initialized = true;
}
bool QLedPlugin::isInitialized() const
{
return initialized;
}
{
return new QLed(parent);
}
{
return u"QLed"_s;
}
{
return u"Lab Widgets"_s; //change this if you want to put the custom widget plugin in another group
}
QIcon QLedPlugin
::icon() const {
return QIcon(":resources/qled.png");
}
QString QLedPlugin
::toolTip() const {
return u"Led Custom widget Plugin fot Qt Designer"_s;
}
QString QLedPlugin
::whatsThis() const {
return u"Led Custom widget Plugin fot Qt Designer"_s;
}
bool QLedPlugin::isContainer() const
{
return false;
}
{
return uR"(
<ui language="c++">
<widget class="QLed" name="qLed">
)"
R"(
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>50</width>
<height>50</height>
</rect>
</property>
")
R"(
<property name="toolTip">
<string>Binary Led</string>
</property>
<property name="value">
<bool>false</bool>
</property>
<property name="whatsThis">
<string>Led widget</string>
</property>
<property name="onColor">
<enum>QLed::Red</enum>
<enum>QLed::Green</enum>
<enum>QLed::Yellow</enum>
<enum>QLed::Grey</enum>
<enum>QLed::Orange</enum>
<enum>QLed::Purple</enum>
<enum>QLed::Blue</enum>
</property>
<property name="offColor">
<enum>QLed::Grey</enum>
</property>
<property name="shape">
<enum>QLed::Circle</enum>
<enum>QLed::Square</enum>
<enum>QLed::Triangle</enum>
<enum>QLed::Rounded</enum>
</property>
</widget>
</ui>
)"_s;
}
QString QLedPlugin
::includeFile() const {
return u"qled.h"_s;
}
#include "qled.h"
#include "qledplugin.h"
#include <QtPlugin>
using namespace Qt::StringLiterals;
// See Cusotm Widget Plugin Example in Qt online documentation to understand the meaning of this file
QLedPlugin::QLedPlugin(QObject *parent)
: QObject(parent)
{
initialized = false;
}
void QLedPlugin::initialize(QDesignerFormEditorInterface * /* core */)
{
if (initialized)
return;
initialized = true;
}
bool QLedPlugin::isInitialized() const
{
return initialized;
}
QWidget *QLedPlugin::createWidget(QWidget *parent)
{
return new QLed(parent);
}
QString QLedPlugin::name() const
{
return u"QLed"_s;
}
QString QLedPlugin::group() const
{
return u"Lab Widgets"_s; //change this if you want to put the custom widget plugin in another group
}
QIcon QLedPlugin::icon() const
{
return QIcon(":resources/qled.png");
}
QString QLedPlugin::toolTip() const
{
return u"Led Custom widget Plugin fot Qt Designer"_s;
}
QString QLedPlugin::whatsThis() const
{
return u"Led Custom widget Plugin fot Qt Designer"_s;
}
bool QLedPlugin::isContainer() const
{
return false;
}
QString QLedPlugin::domXml() const
{
return uR"(
<ui language="c++">
<widget class="QLed" name="qLed">
)"
R"(
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>50</width>
<height>50</height>
</rect>
</property>
")
R"(
<property name="toolTip">
<string>Binary Led</string>
</property>
<property name="value">
<bool>false</bool>
</property>
<property name="whatsThis">
<string>Led widget</string>
</property>
<property name="onColor">
<enum>QLed::Red</enum>
<enum>QLed::Green</enum>
<enum>QLed::Yellow</enum>
<enum>QLed::Grey</enum>
<enum>QLed::Orange</enum>
<enum>QLed::Purple</enum>
<enum>QLed::Blue</enum>
</property>
<property name="offColor">
<enum>QLed::Grey</enum>
</property>
<property name="shape">
<enum>QLed::Circle</enum>
<enum>QLed::Square</enum>
<enum>QLed::Triangle</enum>
<enum>QLed::Rounded</enum>
</property>
</widget>
</ui>
)"_s;
}
QString QLedPlugin::includeFile() const
{
return u"qled.h"_s;
}
To copy to clipboard, switch view to plain text mode
Where am I doing wrong ?
Bookmarks