How to set custom widget form in Qt Designer?

68 Views Asked by At

I created a custom widget in Qt 6.5 using Visual Studio C++ 2022 Community. This widget is a led with various shapes and colors that can be selected in the Qt Designer editor. I can't get selected shape to appear with selected color in Qt Designer. For greater clarity, I attach the code of the created plugin:

`#pragma once

#include <QtWidgets/QWidget>
#include <QtUiPlugin/QDesignerExportWidget>
#include <QtSvg>
#include <QSvgRenderer>

class QColor;

class QDESIGNER_WIDGET_EXPORT Led_0_6_1 : public QWidget
{
    Q_OBJECT

    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 getShape WRITE setShape)

public:
    explicit Led_0_6_1(QWidget *parent = nullptr);
    virtual ~Led_0_6_1();

    bool value() const { return m_value; }

    enum ledColor { Red = 0, Green, Yellow, Grey, Orange, Purple, Blue };
    Q_ENUM(ledColor)
    
    enum ledShape { Circle = 0, Square, Triangle, Rounded };
    Q_ENUM(ledShape)

    ledColor onColor() const { return m_onColor; }
    ledColor offColor() const { return m_offColor; }
    ledShape getShape() const { return m_shape; }

public slots:
    void setValue(bool);
    void setOnColor(ledColor);
    void setOffColor(ledColor);
    void setShape(ledShape);
    void toggleValue();

protected:
    void paintEvent(QPaintEvent* event) override;

    bool m_value;
    ledColor m_onColor, m_offColor;
    int id_Timer;
    ledShape m_shape;
    QStringList shapes;
    QStringList colors;

private:
    QSvgRenderer* renderer;
};`
`#include "Led_0_6_1.h"

#include <QtGlobal>
#include <QtGui>
#include <QtCore/QtPlugin>
#include <QPainter>
#include <QPaintDevice>
#include <QColor>
#include <QPolygon>

Led_0_6_1::Led_0_6_1(QWidget *parent) : QWidget(parent)
{
    m_value = false;
    m_onColor = Red;
    m_offColor = Grey;
    m_shape = Rounded;
    id_Timer = 0;
    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";

    renderer = new QSvgRenderer();
}

Led_0_6_1::~Led_0_6_1()
{
    delete renderer;
}

/*!
  \brief setValue: this method allows to set the led value {true,false}
  \param ledColor newColor
  \return void
*/
void Led_0_6_1::setValue(bool value)
{
    m_value = value;
    update();
}

/*!
  \brief setOnColor: this method allows to change the On color {Red,Green,Yellow,Grey,Orange,Purple,blue}
  \param ledColor newColor
  \return void
*/
void Led_0_6_1::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 Led_0_6_1::setOffColor(ledColor newColor)
{
    m_offColor = newColor;
    update();
}

/*!
  \brief setShape: this method allows to change the led shape {Circle,Square,Triangle,Rounded}
  \param ledShape newShape
  \return void
*/
void Led_0_6_1::setShape(ledShape newShape)
{
    m_shape = newShape;
    qDebug() << "la scelta della forma: " << m_shape;
    update();
}

/*!
  \brief toggleValue: this method toggles the led value
  \param ledColor newColor
  \return void
*/
void Led_0_6_1::toggleValue()
{
    m_value = !m_value;
    update();
}

/*!
  \brief paintEvent: painting method
  \param QPaintEvent *
  \return void
*/
void Led_0_6_1::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event);

    QPainter painter(this);

    QString ledShapeAndColor;
    
    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);
}`

When I'm in Qt Designer to create a form to test my plugin by going to its properties I select the shape and color that the widget should take, but the widget on the form is not updated. The first shape and first color in the list is always set. It would appear that the graphical appearance of the widget is not updated after selecting it from the properties. Based on the code I posted where can the error be ? I read a Qt 6.5 document in https://doc.qt.io/qt-6/designer-ui-file-format.html which tells me that the enums of a given property must be declared only once in the file Led_0_6_1Plugin. cpp (so I carry the file in question, to make it clear what I did). So to solve my problem, that is to be able to select an item from the shape field list and keep that item as a choice, what should I do ?

#include "Led_0_6_1.h"
#include "Led_0_6_1Plugin.h"

#include <QtCore/QtPlugin>

using namespace Qt::StringLiterals;

Led_0_6_1Plugin::Led_0_6_1Plugin(QObject *parent) : QObject(parent)
{
    initialized = false;
}

void Led_0_6_1Plugin::initialize(QDesignerFormEditorInterface * /*core*/)
{
    if (initialized)
        return;

    initialized = true;
}

bool Led_0_6_1Plugin::isInitialized() const
{
    return initialized;
}

QWidget *Led_0_6_1Plugin::createWidget(QWidget *parent)
{
    return new Led_0_6_1(parent);
}

QString Led_0_6_1Plugin::name() const
{
    return "Led_0_6_1";
}

QString Led_0_6_1Plugin::group() const
{
    return "My Plugins";
}

QIcon Led_0_6_1Plugin::icon() const
{
    return QIcon();
}

QString Led_0_6_1Plugin::toolTip() const
{
    return QString();
}

QString Led_0_6_1Plugin::whatsThis() const
{
    return QString();
}

bool Led_0_6_1Plugin::isContainer() const
{
    return false;
}

QString Led_0_6_1Plugin::domXml() const
{
        return uR"(
<ui language="c++">
  <widget class="Led_0_6_1" name="led_0_6_1">
)"        
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>Led_0_6_1::Red</enum>
     </property>
    <property name="offColor">
      <enum>Led_0_6_1::Grey</enum>
    </property>
    <property name="shape">
      <enum>Led_0_6_1::Circle</enum>
    </property>
  </widget>
</ui>
)"_s;
}

QString Led_0_6_1Plugin::includeFile() const
{
    return u"Led_0_6_1.h"_s;
}
0

There are 0 best solutions below