Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to change property value to my custom widget ?
Forum Updated to NodeBB v4.3 + New Features

How to change property value to my custom widget ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 1.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • giorgik63G Offline
    giorgik63G Offline
    giorgik63
    wrote on last edited by
    #1

    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);
    }
    
    SGaistS Christian EhrlicherC 2 Replies Last reply
    0
    • giorgik63G giorgik63

      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);
      }
      
      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      giorgik63G 1 Reply Last reply
      1
      • SGaistS SGaist

        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.

        giorgik63G Offline
        giorgik63G Offline
        giorgik63
        wrote on last edited by
        #3

        @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 ?

        SGaistS 1 Reply Last reply
        0
        • giorgik63G giorgik63

          @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 ?

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Well, as I wrote it. It's the simpler and cleaner way to do it.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          giorgik63G 1 Reply Last reply
          0
          • SGaistS SGaist

            Well, as I wrote it. It's the simpler and cleaner way to do it.

            giorgik63G Offline
            giorgik63G Offline
            giorgik63
            wrote on last edited by
            #5

            @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) ?

            1 Reply Last reply
            0
            • giorgik63G giorgik63

              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);
              }
              
              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by Christian Ehrlicher
              #6

              @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.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              giorgik63G 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @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.

                giorgik63G Offline
                giorgik63G Offline
                giorgik63
                wrote on last edited by
                #7

                @Christian-Ehrlicher So how should I write, based on the explanations I said in previous answers ?

                Christian EhrlicherC 1 Reply Last reply
                0
                • giorgik63G giorgik63

                  @Christian-Ehrlicher So how should I write, based on the explanations I said in previous answers ?

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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?

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  giorgik63G 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @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?

                    giorgik63G Offline
                    giorgik63G Offline
                    giorgik63
                    wrote on last edited by
                    #9

                    @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 EhrlicherC 1 Reply Last reply
                    0
                    • giorgik63G giorgik63

                      @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 EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @giorgik63 said in How to change property value to my custom widget ?:

                      but nothing changes

                      This code does not even compile.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      giorgik63G 1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        @giorgik63 said in How to change property value to my custom widget ?:

                        but nothing changes

                        This code does not even compile.

                        giorgik63G Offline
                        giorgik63G Offline
                        giorgik63
                        wrote on last edited by giorgik63
                        #11

                        @Christian-Ehrlicher In what sense ? It compiles just fine for me.

                        JonBJ 1 Reply Last reply
                        0
                        • giorgik63G giorgik63

                          @Christian-Ehrlicher In what sense ? It compiles just fine for me.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #12

                          @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::ledShape has been defined.
                          • This defines a method in the QLedNew class. No such class has been declared.
                          • The method QImageSvg::shape(), declared in the header file for class QImageSvg, has not been defined anywhere.
                          • In its body there is no such variable as shape for the return statement.

                          If this is really the code you have, with other stuff to make it compile, perhaps it has some relationship to your problem....

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved