How to change property value to my custom widget ?
-
I created my own widget named QImageSvg to be included in the list of available widgets in Qt Designer (I'm working with Qt 6.5 version using Visual Studio C++ 2022 Community C++ compiler). I created a property called imageShape in the QImageSvg widget.
... Q_PROPERTY(imageShape shape MEMBER m_shape NOTIFY m_shapeChanged) signals: void m_shapeChanged(const QString& newShape); public: explicit QImageSvg(QWidget *parent = nullptr); enum imageShape { Circle = 0, Square, Triangle, Rounded }; Q_ENUM(imageShape) protected: imageShape m_shape; ...I would like to make sure that in the properties of my QImageSvg widget, when I click on the imageShape item, I can select an item (for example Triangle) and therefore see Triangle as the new value. In this way I change the graphic image of my widget with the Triangle shape which is read from the triang_orange.svg file placed in the Resources folder of the widget project and registered in the QImageSvg.qrc file in this way:
<RCC> <qresource prefix="/"> <file>Resources/circle_green.svg</file> <file>Resources/circle_grey.svg</file> <file>Resources/circle_orange.svg</file> <file>Resources/circle_purple.svg</file> <file>Resources/circle_red.svg</file> <file>Resources/circle_yellow.svg</file> <file>Resources/circle_blue.svg</file> <file>Resources/square_blue.svg</file> <file>Resources/square_green.svg</file> <file>Resources/square_grey.svg</file> <file>Resources/square_orange.svg</file> <file>Resources/square_purple.svg</file> <file>Resources/square_red.svg</file> <file>Resources/square_yellow.svg</file> <file>Resources/triang_blue.svg</file> <file>Resources/triang_green.svg</file> <file>Resources/triang_grey.svg</file> <file>Resources/triang_orange.svg</file> <file>Resources/triang_purple.svg</file> <file>Resources/triang_red.svg</file> <file>Resources/triang_yellow.svg</file> <file>Resources/round_blue.svg</file> <file>Resources/round_green.svg</file> <file>Resources/round_grey.svg</file> <file>Resources/round_purple.svg</file> <file>Resources/round_red.svg</file> <file>Resources/round_yellow.svg</file> <file>Resources/round_orange.svg</file> </qresource> </RCC>What should I write in the QImageSvg.cpp code to read the new value of the imageShape property and consequently see the graphic shape of the widget changed ( using the paintEvent(QPaintEvent * event) ) method?
void QImageSvg::paintEvent(QPaintEvent * event) { QPixmap pix(strShape); setPixmap(pix); } -
I created my own widget named QImageSvg to be included in the list of available widgets in Qt Designer (I'm working with Qt 6.5 version using Visual Studio C++ 2022 Community C++ compiler). I created a property called imageShape in the QImageSvg widget.
... Q_PROPERTY(imageShape shape MEMBER m_shape NOTIFY m_shapeChanged) signals: void m_shapeChanged(const QString& newShape); public: explicit QImageSvg(QWidget *parent = nullptr); enum imageShape { Circle = 0, Square, Triangle, Rounded }; Q_ENUM(imageShape) protected: imageShape m_shape; ...I would like to make sure that in the properties of my QImageSvg widget, when I click on the imageShape item, I can select an item (for example Triangle) and therefore see Triangle as the new value. In this way I change the graphic image of my widget with the Triangle shape which is read from the triang_orange.svg file placed in the Resources folder of the widget project and registered in the QImageSvg.qrc file in this way:
<RCC> <qresource prefix="/"> <file>Resources/circle_green.svg</file> <file>Resources/circle_grey.svg</file> <file>Resources/circle_orange.svg</file> <file>Resources/circle_purple.svg</file> <file>Resources/circle_red.svg</file> <file>Resources/circle_yellow.svg</file> <file>Resources/circle_blue.svg</file> <file>Resources/square_blue.svg</file> <file>Resources/square_green.svg</file> <file>Resources/square_grey.svg</file> <file>Resources/square_orange.svg</file> <file>Resources/square_purple.svg</file> <file>Resources/square_red.svg</file> <file>Resources/square_yellow.svg</file> <file>Resources/triang_blue.svg</file> <file>Resources/triang_green.svg</file> <file>Resources/triang_grey.svg</file> <file>Resources/triang_orange.svg</file> <file>Resources/triang_purple.svg</file> <file>Resources/triang_red.svg</file> <file>Resources/triang_yellow.svg</file> <file>Resources/round_blue.svg</file> <file>Resources/round_green.svg</file> <file>Resources/round_grey.svg</file> <file>Resources/round_purple.svg</file> <file>Resources/round_red.svg</file> <file>Resources/round_yellow.svg</file> <file>Resources/round_orange.svg</file> </qresource> </RCC>What should I write in the QImageSvg.cpp code to read the new value of the imageShape property and consequently see the graphic shape of the widget changed ( using the paintEvent(QPaintEvent * event) ) method?
void QImageSvg::paintEvent(QPaintEvent * event) { QPixmap pix(strShape); setPixmap(pix); }Hi,
Based on your code, you should use a standard slot and set the pixmap there when the "shape" has changed. There's no need to reimplement paintEvent.
-
Hi,
Based on your code, you should use a standard slot and set the pixmap there when the "shape" has changed. There's no need to reimplement paintEvent.
-
@SGaist Alright, this is useful to me for the next stage of the code. But what about the actual value change of the imageShape property based on my selection, how should I do it ?
Well, as I wrote it. It's the simpler and cleaner way to do it.
-
@SGaist Maybe I didn't explain myself well (sorry for my bad english, google translator trip). At present (see code I reported), when I'm in Qt Designer, I select my QImageSVG plugin and go to the right panel of the Qt Designer IDE (QImageSVG properties). In this panel I click on imageShape and from its list I select the Triangle item (for example). After selection I would expect to see the Triangle entry active, but as soon as I hit Enter it returns me to the first entry in the list, i.e. Circle. My question is: how to make the value of the property change to the value just selected (even after confirming the selection with Enter) ?
-
I created my own widget named QImageSvg to be included in the list of available widgets in Qt Designer (I'm working with Qt 6.5 version using Visual Studio C++ 2022 Community C++ compiler). I created a property called imageShape in the QImageSvg widget.
... Q_PROPERTY(imageShape shape MEMBER m_shape NOTIFY m_shapeChanged) signals: void m_shapeChanged(const QString& newShape); public: explicit QImageSvg(QWidget *parent = nullptr); enum imageShape { Circle = 0, Square, Triangle, Rounded }; Q_ENUM(imageShape) protected: imageShape m_shape; ...I would like to make sure that in the properties of my QImageSvg widget, when I click on the imageShape item, I can select an item (for example Triangle) and therefore see Triangle as the new value. In this way I change the graphic image of my widget with the Triangle shape which is read from the triang_orange.svg file placed in the Resources folder of the widget project and registered in the QImageSvg.qrc file in this way:
<RCC> <qresource prefix="/"> <file>Resources/circle_green.svg</file> <file>Resources/circle_grey.svg</file> <file>Resources/circle_orange.svg</file> <file>Resources/circle_purple.svg</file> <file>Resources/circle_red.svg</file> <file>Resources/circle_yellow.svg</file> <file>Resources/circle_blue.svg</file> <file>Resources/square_blue.svg</file> <file>Resources/square_green.svg</file> <file>Resources/square_grey.svg</file> <file>Resources/square_orange.svg</file> <file>Resources/square_purple.svg</file> <file>Resources/square_red.svg</file> <file>Resources/square_yellow.svg</file> <file>Resources/triang_blue.svg</file> <file>Resources/triang_green.svg</file> <file>Resources/triang_grey.svg</file> <file>Resources/triang_orange.svg</file> <file>Resources/triang_purple.svg</file> <file>Resources/triang_red.svg</file> <file>Resources/triang_yellow.svg</file> <file>Resources/round_blue.svg</file> <file>Resources/round_green.svg</file> <file>Resources/round_grey.svg</file> <file>Resources/round_purple.svg</file> <file>Resources/round_red.svg</file> <file>Resources/round_yellow.svg</file> <file>Resources/round_orange.svg</file> </qresource> </RCC>What should I write in the QImageSvg.cpp code to read the new value of the imageShape property and consequently see the graphic shape of the widget changed ( using the paintEvent(QPaintEvent * event) ) method?
void QImageSvg::paintEvent(QPaintEvent * event) { QPixmap pix(strShape); setPixmap(pix); }@giorgik63 said in How to change property value to my custom widget ?:
Q_PROPERTY(imageShape shape MEMBER m_shape NOTIFY m_shapeChanged)
void m_shapeChanged(const QString& newShape);
imageShape m_shape;Your property is an enum but you emit a QString for the property change? How should this work? Also the signal name is somewhat... strange.
-
@giorgik63 said in How to change property value to my custom widget ?:
Q_PROPERTY(imageShape shape MEMBER m_shape NOTIFY m_shapeChanged)
void m_shapeChanged(const QString& newShape);
imageShape m_shape;Your property is an enum but you emit a QString for the property change? How should this work? Also the signal name is somewhat... strange.
@Christian-Ehrlicher So how should I write, based on the explanations I said in previous answers ?
-
@Christian-Ehrlicher So how should I write, based on the explanations I said in previous answers ?
@giorgik63 said in How to change property value to my custom widget ?:
So how should I write,
When the property has the type 'imageShape' don't you think the signal which should be emitted when exactly this property change use this enum?
-
@giorgik63 said in How to change property value to my custom widget ?:
So how should I write,
When the property has the type 'imageShape' don't you think the signal which should be emitted when exactly this property change use this enum?
@Christian-Ehrlicher I already tried writing like this:
... Q_PROPERTY(imageShape shape READ shape WRITE setShape) ... imageShape shape() const; void setShape(const imageShape& shape); protected: imageShape m_shape; ... QImageSvg::ledShape QLedNew::shape() const { return shape; } void QImageSvg::setShape(const imageShape& shape) { m_shape = shape; }but nothing changes, I can't get the Triangle selected value set as a property, I always have Circle. How come ?
-
@Christian-Ehrlicher I already tried writing like this:
... Q_PROPERTY(imageShape shape READ shape WRITE setShape) ... imageShape shape() const; void setShape(const imageShape& shape); protected: imageShape m_shape; ... QImageSvg::ledShape QLedNew::shape() const { return shape; } void QImageSvg::setShape(const imageShape& shape) { m_shape = shape; }but nothing changes, I can't get the Triangle selected value set as a property, I always have Circle. How come ?
@giorgik63 said in How to change property value to my custom widget ?:
but nothing changes
This code does not even compile.
-
@giorgik63 said in How to change property value to my custom widget ?:
but nothing changes
This code does not even compile.
@Christian-Ehrlicher In what sense ? It compiles just fine for me.
-
@Christian-Ehrlicher In what sense ? It compiles just fine for me.
@giorgik63 said in How to change property value to my custom widget ?:
It compiles just fine for me.
Really? Let's look at
QImageSvg::ledShape QLedNew::shape() const { return shape; }- No type
QImageSvg::ledShapehas been defined. - This defines a method in the
QLedNewclass. No such class has been declared. - The method
QImageSvg::shape(), declared in the header file for classQImageSvg, has not been defined anywhere. - In its body there is no such variable as
shapefor thereturnstatement.
If this is really the code you have, with other stuff to make it compile, perhaps it has some relationship to your problem....
- No type