Originally Posted by
d_stranz
2a - Insert a QMessageBox warning in the paintEvent where you try to load the svg file for the color and shape. If the load fails, pop up the warning.
...
In the case of 2a, it is creating the widget but is unable to load the SVG to perform the rendering. Again, this could be a problem with the QSvg DLL.
I rewrote the code again and post it for you so you can see it too. I put the QMessageBox in QNewLed:aintEvent(..) at what point ?
QNewLed.h:
#pragma once
#include <QtWidgets/QWidget>
#include <QtUiPlugin/QDesignerExportWidget>
#include <QtSvg>
#include <QSvgRenderer>
class QDESIGNER_WIDGET_EXPORT QNewLed
: public QWidget{
Q_OBJECT
Q_PROPERTY(ledShape shape READ getShape WRITE setShape)
public:
explicit QNewLed
(QWidget *parent
= nullptr
);
virtual ~QNewLed();
enum ledShape { Circle, Square, Triangle, Rounded };
Q_ENUM(ledShape)
public slots:
ledShape getShape() const { return m_shape; }
void setShape(ledShape);
protected:
ledShape m_shape;
private:
};
#pragma once
#include <QtWidgets/QWidget>
#include <QtUiPlugin/QDesignerExportWidget>
#include <QtSvg>
#include <QSvgRenderer>
class QDESIGNER_WIDGET_EXPORT QNewLed : public QWidget
{
Q_OBJECT
Q_PROPERTY(ledShape shape READ getShape WRITE setShape)
public:
explicit QNewLed(QWidget *parent = nullptr);
virtual ~QNewLed();
enum ledShape { Circle, Square, Triangle, Rounded };
Q_ENUM(ledShape)
public slots:
ledShape getShape() const { return m_shape; }
void setShape(ledShape);
protected:
void paintEvent(QPaintEvent* event) override;
ledShape m_shape;
QStringList m_shapes;
private:
QSvgRenderer* renderer;
};
To copy to clipboard, switch view to plain text mode
QNewLed.cpp:
#include "QNewLed.h"
{
m_shapes << ":/resources/circle_green.svg" << ":/resources/square_orange.svg" << ":/resources/triang_blue.svg" << ":/resources/round_yellow.svg";
setMinimumSize
(QSize(50,
50));
}
QNewLed::~QNewLed()
{
delete renderer;
}
void QNewLed::setShape(ledShape newShape)
{
m_shape = newShape;
update();
}
{
Q_UNUSED(event);
ledShapeAndColor = m_shapes[m_shape];
painter.
setRenderHint(QPainter::Antialiasing,
true);
renderer->load(ledShapeAndColor);
renderer->render(&painter);
}
#include "QNewLed.h"
QNewLed::QNewLed(QWidget *parent) : QWidget(parent), m_shape(Rounded)
{
m_shapes << ":/resources/circle_green.svg" << ":/resources/square_orange.svg" << ":/resources/triang_blue.svg" << ":/resources/round_yellow.svg";
setMinimumSize(QSize(50, 50));
renderer = new QSvgRenderer();
}
QNewLed::~QNewLed()
{
delete renderer;
}
void QNewLed::setShape(ledShape newShape)
{
m_shape = newShape;
update();
}
void QNewLed::paintEvent(QPaintEvent* event)
{
Q_UNUSED(event);
QPainter painter(this);
QString ledShapeAndColor;
ledShapeAndColor = m_shapes[m_shape];
painter.setRenderHint(QPainter::Antialiasing, true);
renderer->load(ledShapeAndColor);
renderer->render(&painter);
}
To copy to clipboard, switch view to plain text mode
QNewLedPlugin.h:
#pragma once
#include <QtUiPlugin/QDesignerCustomWidgetInterface>
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface" FILE "qnewledplugin.json")
public:
QNewLedPlugin
(QObject *parent
= nullptr
);
bool isContainer() const override;
bool isInitialized() const override;
QIcon icon
() const override;
QString includeFile
() const override;
QString whatsThis
() const override;
private:
bool initialized;
};
#pragma once
#include <QtUiPlugin/QDesignerCustomWidgetInterface>
class QNewLedPlugin : public QObject, public QDesignerCustomWidgetInterface
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface" FILE "qnewledplugin.json")
Q_INTERFACES(QDesignerCustomWidgetInterface)
public:
QNewLedPlugin(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);
void initialize(QDesignerFormEditorInterface *core);
private:
bool initialized;
};
To copy to clipboard, switch view to plain text mode
QNewLedPlugin.cpp:
#include "QNewLed.h"
#include "QNewLedPlugin.h"
#include <QtCore/QtPlugin>
{
initialized = false;
}
{
if (initialized)
return;
initialized = true;
}
bool QNewLedPlugin::isInitialized() const
{
return initialized;
}
{
return new QNewLed(parent);
}
QString QNewLedPlugin
::name() const {
return "QNewLed";
}
QString QNewLedPlugin
::group() const {
return "I Miei Plugin";
}
QIcon QNewLedPlugin
::icon() const {
}
QString QNewLedPlugin
::toolTip() const {
}
QString QNewLedPlugin
::whatsThis() const {
}
bool QNewLedPlugin::isContainer() const
{
return false;
}
QString QNewLedPlugin
::domXml() const {
return "<widget class=\"QNewLed\" name=\"led\">\n"
" <property name=\"geometry\">\n"
" <rect>\n"
" <x>0</x>\n"
" <y>0</y>\n"
" <width>50</width>\n"
" <height>50</height>\n"
" </rect>\n"
" </property>\n"
" <property name=\"toolTip\" >\n"
" <string>Binary Led</string>\n"
" </property>\n"
" <property name=\"whatsThis\" >\n"
" <string>Led widget</string>\n"
" </property>\n"
" <property name=\"shape\" >\n"
" <enum>QNewLed::Circle</enum>\n"
" </property>\n"
"</widget>\n";
}
QString QNewLedPlugin
::includeFile() const {
return "QNewLed.h";
}
#include "QNewLed.h"
#include "QNewLedPlugin.h"
#include <QtCore/QtPlugin>
QNewLedPlugin::QNewLedPlugin(QObject *parent) : QObject(parent)
{
initialized = false;
}
void QNewLedPlugin::initialize(QDesignerFormEditorInterface * /*core*/)
{
if (initialized)
return;
initialized = true;
}
bool QNewLedPlugin::isInitialized() const
{
return initialized;
}
QWidget *QNewLedPlugin::createWidget(QWidget *parent)
{
return new QNewLed(parent);
}
QString QNewLedPlugin::name() const
{
return "QNewLed";
}
QString QNewLedPlugin::group() const
{
return "I Miei Plugin";
}
QIcon QNewLedPlugin::icon() const
{
return QIcon();
}
QString QNewLedPlugin::toolTip() const
{
return QString();
}
QString QNewLedPlugin::whatsThis() const
{
return QString();
}
bool QNewLedPlugin::isContainer() const
{
return false;
}
QString QNewLedPlugin::domXml() const
{
return "<widget class=\"QNewLed\" name=\"led\">\n"
" <property name=\"geometry\">\n"
" <rect>\n"
" <x>0</x>\n"
" <y>0</y>\n"
" <width>50</width>\n"
" <height>50</height>\n"
" </rect>\n"
" </property>\n"
" <property name=\"toolTip\" >\n"
" <string>Binary Led</string>\n"
" </property>\n"
" <property name=\"whatsThis\" >\n"
" <string>Led widget</string>\n"
" </property>\n"
" <property name=\"shape\" >\n"
" <enum>QNewLed::Circle</enum>\n"
" </property>\n"
"</widget>\n";
}
QString QNewLedPlugin::includeFile() const
{
return "QNewLed.h";
}
To copy to clipboard, switch view to plain text mode
This new version is a little more simplified than the previous one, just to understand where I'm wrong.
Bookmarks