Results 21 to 28 of 28

Thread: How to change widget shape in QtDesigner ?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to change widget shape in QtDesigner ?

    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:
    Qt Code:
    1. #pragma once
    2.  
    3. #include <Qt>
    4. #include <QtWidgets/QWidget>
    5. #include <QtUiPlugin/QDesignerExportWidget>
    6.  
    7.  
    8. // My Qt designer widget plugin class
    9.  
    10. class QColor;
    11.  
    12. class QDESIGNER_WIDGET_EXPORT QLed : public QWidget
    13. {
    14. Q_OBJECT
    15. Q_ENUMS (ledColor)
    16. Q_ENUMS (ledShape)
    17. Q_PROPERTY(bool value READ value WRITE setValue)
    18. Q_PROPERTY(ledColor onColor READ onColor WRITE setOnColor)
    19. Q_PROPERTY(ledColor offColor READ offColor WRITE setOffColor)
    20. //Q_PROPERTY(ledShape shape READ shape WRITE setShape NOTIFY shapeChanged)
    21. Q_PROPERTY(ledShape shape READ shape WRITE setShape)
    22.  
    23. public:
    24. explicit QLed(QWidget *parent = 0);
    25.  
    26. bool value() const { return m_value; }
    27.  
    28. enum ledColor { Red = 0, Green, Yellow, Grey, Orange, Purple, Blue };
    29. enum ledShape { Circle = 0, Square, Triangle, Rounded };
    30.  
    31. ledColor onColor() const { return m_onColor; }
    32. ledColor offColor() const { return m_offColor; }
    33. ledShape shape() const { return m_shape; }
    34.  
    35. public slots:
    36. void setValue(bool);
    37. void setOnColor(ledColor);
    38. void setOffColor(ledColor);
    39. void setShape(ledShape);
    40. void toggleValue();
    41.  
    42. signals:
    43. void shapeChanged(ledShape);
    44.  
    45. protected:
    46. bool m_value;
    47.  
    48. ledColor m_onColor, m_offColor;
    49.  
    50. int id_Timer;
    51.  
    52. ledShape m_shape;
    53.  
    54. QStringList shapes;
    55. QStringList colors;
    56.  
    57. void paintEvent(QPaintEvent *event) override;
    58. };
    To copy to clipboard, switch view to plain text mode 

    Below I put the qled.cpp file of the widget:
    Qt Code:
    1. #include <QColor>
    2. #include <QtGlobal>
    3. #include <QtGui>
    4. #include <QPolygon>
    5. #include <QtSvg>
    6. #include <QSvgRenderer>
    7. #include "qled.h"
    8.  
    9. /*!
    10.   \brief QLed: this is the QLed constructor.
    11.   \param parent: The Parent Widget
    12. */
    13. QLed::QLed(QWidget *parent) : QWidget(parent)
    14. {
    15. m_value = false;
    16. m_onColor = Red;
    17. m_offColor = Grey;
    18. m_shape = Circle;
    19.  
    20. setMinimumSize(QSize(50,50));
    21.  
    22. shapes << ":/resources/circle_" << ":/resources/square_" << ":/resources/triang_" << ":/resources/round_";
    23. colors << "red.svg" << "green.svg" << "yellow.svg" << "grey.svg" << "orange.svg" << "purple.svg" << "blue.svg";
    24. }
    25.  
    26.  
    27. /*!
    28.   \brief paintEvent: painting method
    29.   \param QPaintEvent *
    30.   \return void
    31. */
    32. void QLed::paintEvent(QPaintEvent *)
    33. {
    34. QSvgRenderer *renderer = new QSvgRenderer();
    35. QString ledShapeAndColor;
    36. QPainter painter(this);
    37. painter.setRenderHint(QPainter::Antialiasing, true);
    38.  
    39. ledShapeAndColor = shapes[m_shape];
    40.  
    41. if(m_value)
    42. ledShapeAndColor.append(colors[m_onColor]);
    43. else
    44. ledShapeAndColor.append(colors[m_offColor]);
    45.  
    46. renderer->load(ledShapeAndColor);
    47. renderer->render(&painter);
    48. renderer = nullptr;
    49. delete renderer;
    50. }
    51.  
    52. /*!
    53.   \brief setOnColor: this method allows to change the On color {Red,Green,Yellow,Grey,Orange,Purple,blue}
    54.   \param ledColor newColor
    55.   \return void
    56. */
    57. void QLed::setOnColor(ledColor newColor)
    58. {
    59. m_onColor = newColor;
    60. update();
    61. }
    62.  
    63.  
    64. /*!
    65.   \brief setOffColor: this method allows to change the Off color {Red,Green,Yellow,Grey,Orange,Purple,blue}
    66.   \param ledColor newColor
    67.   \return void
    68. */
    69. void QLed::setOffColor(ledColor newColor)
    70. {
    71. m_offColor = newColor;
    72. update();
    73. }
    74.  
    75.  
    76. /*!
    77.   \brief setShape: this method allows to change the led shape {Circle,Square,Triangle,Rounded rectangle}
    78.   \param ledColor newColor
    79.   \return void
    80. */
    81. void QLed::setShape(ledShape newShape)
    82. {
    83. m_shape = newShape;
    84. update();
    85. }
    86.  
    87. /*!
    88.   \brief setValue: this method allows to set the led value {true,false}
    89.   \param ledColor newColor
    90.   \return void
    91. */
    92. void QLed::setValue(bool value)
    93. {
    94. m_value = value;
    95. update();
    96. }
    97.  
    98.  
    99. /*!
    100.   \brief toggleValue: this method toggles the led value
    101.   \param ledColor newColor
    102.   \return void
    103. */
    104. void QLed::toggleValue()
    105. {
    106. m_value =! m_value;
    107. update();
    108. }
    To copy to clipboard, switch view to plain text mode 

    Below I put the qledplugin.h file of the widget:
    Qt Code:
    1. #pragma once
    2.  
    3. #include <QDesignerCustomWidgetInterface>
    4.  
    5. // See Cusotm Widget Plugin Example in Qt online documentation to understand the meaning of this file
    6.  
    7.  
    8. class QLedPlugin : public QObject, public QDesignerCustomWidgetInterface
    9. {
    10. Q_OBJECT
    11. Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface")
    12.  
    13. public:
    14. QLedPlugin(QObject *parent = 0);
    15.  
    16. bool isContainer() const override;
    17. bool isInitialized() const override;
    18.  
    19. QIcon icon() const override;
    20.  
    21. QString domXml() const override;
    22. QString group() const override;
    23. QString includeFile() const override;
    24. QString name() const override;
    25. QString toolTip() const override;
    26. QString whatsThis() const override;
    27.  
    28. QWidget *createWidget(QWidget *parent) override;
    29.  
    30. void initialize(QDesignerFormEditorInterface *core) override;
    31.  
    32. private:
    33. bool initialized = false;
    34. };
    To copy to clipboard, switch view to plain text mode 

    Below I put the qledplugin.cpp file of the widget:
    Qt Code:
    1. #include "qled.h"
    2. #include "qledplugin.h"
    3.  
    4. #include <QtPlugin>
    5.  
    6. using namespace Qt::StringLiterals;
    7.  
    8. // See Cusotm Widget Plugin Example in Qt online documentation to understand the meaning of this file
    9.  
    10. QLedPlugin::QLedPlugin(QObject *parent)
    11. : QObject(parent)
    12. {
    13. initialized = false;
    14. }
    15.  
    16. void QLedPlugin::initialize(QDesignerFormEditorInterface * /* core */)
    17. {
    18. if (initialized)
    19. return;
    20.  
    21. initialized = true;
    22. }
    23.  
    24. bool QLedPlugin::isInitialized() const
    25. {
    26. return initialized;
    27. }
    28.  
    29. QWidget *QLedPlugin::createWidget(QWidget *parent)
    30. {
    31. return new QLed(parent);
    32. }
    33.  
    34. QString QLedPlugin::name() const
    35. {
    36. return u"QLed"_s;
    37. }
    38.  
    39. QString QLedPlugin::group() const
    40. {
    41. return u"Lab Widgets"_s; //change this if you want to put the custom widget plugin in another group
    42. }
    43.  
    44. QIcon QLedPlugin::icon() const
    45. {
    46. return QIcon(":resources/qled.png");
    47. }
    48.  
    49. QString QLedPlugin::toolTip() const
    50. {
    51. return u"Led Custom widget Plugin fot Qt Designer"_s;
    52. }
    53.  
    54. QString QLedPlugin::whatsThis() const
    55. {
    56. return u"Led Custom widget Plugin fot Qt Designer"_s;
    57. }
    58.  
    59. bool QLedPlugin::isContainer() const
    60. {
    61. return false;
    62. }
    63.  
    64. QString QLedPlugin::domXml() const
    65. {
    66. return uR"(
    67. <ui language="c++">
    68. <widget class="QLed" name="qLed">
    69. )"
    70.  
    71. R"(
    72. <property name="geometry">
    73. <rect>
    74. <x>0</x>
    75. <y>0</y>
    76. <width>50</width>
    77. <height>50</height>
    78. </rect>
    79. </property>
    80. ")
    81.  
    82. R"(
    83. <property name="toolTip">
    84. <string>Binary Led</string>
    85. </property>
    86. <property name="value">
    87. <bool>false</bool>
    88. </property>
    89. <property name="whatsThis">
    90. <string>Led widget</string>
    91. </property>
    92. <property name="onColor">
    93. <enum>QLed::Red</enum>
    94. <enum>QLed::Green</enum>
    95. <enum>QLed::Yellow</enum>
    96. <enum>QLed::Grey</enum>
    97. <enum>QLed::Orange</enum>
    98. <enum>QLed::Purple</enum>
    99. <enum>QLed::Blue</enum>
    100. </property>
    101. <property name="offColor">
    102. <enum>QLed::Grey</enum>
    103. </property>
    104. <property name="shape">
    105. <enum>QLed::Circle</enum>
    106. <enum>QLed::Square</enum>
    107. <enum>QLed::Triangle</enum>
    108. <enum>QLed::Rounded</enum>
    109. </property>
    110. </widget>
    111. </ui>
    112. )"_s;
    113. }
    114.  
    115. QString QLedPlugin::includeFile() const
    116. {
    117. return u"qled.h"_s;
    118. }
    To copy to clipboard, switch view to plain text mode 

    Where am I doing wrong ?
    Attached Images Attached Images

Similar Threads

  1. QCursor change shape in Qt 5
    By cic1988 in forum Qt Programming
    Replies: 5
    Last Post: 4th March 2015, 20:49
  2. Can I change the shape of the form in QML?
    By chong_kimkeang in forum Newbie
    Replies: 1
    Last Post: 7th November 2012, 17:51
  3. how to change shape of Qwidget??
    By anupamgee in forum Qt Programming
    Replies: 4
    Last Post: 29th June 2009, 09:54
  4. Change the shape of a frame.
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2007, 06:40
  5. How to change shape fast
    By nileshsince1980 in forum Qt Programming
    Replies: 9
    Last Post: 18th October 2007, 05:49

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.