How to manage choice item property my plugin in Qt 6.5
-
I'm using Visual Studio C++ 2022 Community with VS Tools for Qt. I created a plugin named Led_0_6_1 based on the old project by P. Sereno. The plugin works, the only problem it presents when you want to change the shape of the LED from the Qt Designer panel (see attached image). When I select for example Triangle, it doesn't change shape and always remains Circle. The problem is due to the fact that every change made in the editor of my widget properties (in the specific case shape) does not update the .ui file which therefore continues to show me the only one present in the .ui file which is precisely Circle. How do I get the .ui file to update after selecting Triangle in the shape property ?
-
Led_0_6_1Plugin.h:
#pragma once #include <QtUiPlugin/QDesignerCustomWidgetInterface> class Led_0_6_1Plugin : public QObject, public QDesignerCustomWidgetInterface { Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface" FILE "led_0_6_1plugin.json") Q_INTERFACES(QDesignerCustomWidgetInterface) public: Led_0_6_1Plugin(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; };
Led_0_6_1Plugin.cpp:
#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; }
-
Hi,
I dont know where exactly you got this code from and who P. Sereno is, but I found this
According to your version number 0_6_1, the plugin from my link is one patch "newer", but still quite old.
The property list is used to change between different styles and colors.
I checked the code and how this was done in 0_6_2.
There is a SVG vector graphics file for every combination of shape + color. Every color and every shape is added to a list from where you select one index, which represents the filename of one graphics file.
These files were added to the Qt resource system and via your menu, you only change the file, which is used to paint/render the LED.The files you are showing are not the important parts.
Check your
Led_0_6_1.cpp
Edit:
Ok, found the name "P. Sereno" in the credits.
This is the
led.cpp
code file from version 0.6.2
(the full project can be found following my link, if something is missing in your version and the shapes dont work/change)/*************************************************************************** * Copyright (C) 2010 by P. Sereno * * http://www.sereno-online.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License * * version 2.1 as published by the Free Software Foundation * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Lesser General Public License for more details. * * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. * ***************************************************************************/ #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; 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(); } QLed::~QLed() { delete renderer; } /*! \brief paintEvent: painting method \param QPaintEvent * \return void */ void QLed::paintEvent(QPaintEvent *) { 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); } /*! \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(); return; }
-
-
Hello Pl45m4, thank you for the time you gave me. I also found version 0.6.2, but it doesn't change much from the code I used. The problem apparently doesn't seem to be the code but the development environment I use: 6.5.0_msvc2019_64, Qt Creator based on Visual Studio C++ 2019 (the version I have of Visual Studio C++ is the 2022 Community) with Extensions | Qt VS Tools. This was an old Qt issue already dating back to 4.7.x with the same plugin I presented in the post. I was able to get it to work properly in Qt Designer based on MinGW 4.8. Others had tried to use it in Visual Studio C++ but they couldn't set the led shape from the Qt Designer properties, just like it happens to me. By chance, were you able to make it work in Qt Designer (the change of the form corresponds to a different display of the LED on the form you are drawing in Qt Designer)? If yes, can you tell me which version of Qt Creator, Qt Designer you use and with which compiler-environment (MinGw which version, Visual Studio C++ which version) ?