Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Update Q_PROPERTY: how does the notify signal work?
QtWS25 Last Chance

Update Q_PROPERTY: how does the notify signal work?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
14 Posts 7 Posters 11.9k Views
  • 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.
  • M Offline
    M Offline
    maxwell31
    wrote on last edited by
    #1

    Hi,

    I have a c++ class, which exposes a QList<QObject*> as a model and also has a int property:

      Q_PROPERTY(QVariant customModel READ getModel NOTIFY customModelChanged)
      Q_PROPERTY(int currentValue READ getCurrentValue NOTIFY customModelChanged)
    

    customModelChanged gets emitted whenever the model is changed. In qml I have a property set to currentValue, however, I can see (having a breakpoint in getCurrentValue during debugging) that getCurrentValue is not called when customModelChanged is emitted. Do I missunderstand somethinbg?

    ODБOïO 1 Reply Last reply
    0
    • M maxwell31

      Hi,

      I have a c++ class, which exposes a QList<QObject*> as a model and also has a int property:

        Q_PROPERTY(QVariant customModel READ getModel NOTIFY customModelChanged)
        Q_PROPERTY(int currentValue READ getCurrentValue NOTIFY customModelChanged)
      

      customModelChanged gets emitted whenever the model is changed. In qml I have a property set to currentValue, however, I can see (having a breakpoint in getCurrentValue during debugging) that getCurrentValue is not called when customModelChanged is emitted. Do I missunderstand somethinbg?

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      Hi,

      getCurrentValue is not supposed to be called when currentValueChanged is emited

      You generally use getCurrentValue from qml side to get the value of a private property (like accessor)

      class MachineBackend : public QObject
      {
          Q_OBJECT
      public:
          Q_PROPERTY(QString message READ getMessage  WRITE setMessage NOTIFY messageChanged)
      
      QString getMessage(){
      return m_message;
      }
      void setMessage(QString newMessage){
      m_message=newMessage;
      emit messageChanged() // Will notify en ensure value is refreshed
      }
      private :
       QString m_message;
      

      then in qml :

      Text{
      text : myObj.getMessage // will refresh
      text : myObj.message
      }

      SGaistS GrecKoG J.HilkJ 3 Replies Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        in addition to previous post, you should always follow <propertyName> and propertyNameChanged signal.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        2
        • ODБOïO ODБOï

          Hi,

          getCurrentValue is not supposed to be called when currentValueChanged is emited

          You generally use getCurrentValue from qml side to get the value of a private property (like accessor)

          class MachineBackend : public QObject
          {
              Q_OBJECT
          public:
              Q_PROPERTY(QString message READ getMessage  WRITE setMessage NOTIFY messageChanged)
          
          QString getMessage(){
          return m_message;
          }
          void setMessage(QString newMessage){
          m_message=newMessage;
          emit messageChanged() // Will notify en ensure value is refreshed
          }
          private :
           QString m_message;
          

          then in qml :

          Text{
          text : myObj.getMessage // will refresh
          text : myObj.message
          }

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

          Hi,

          Some small yet useful improvements:

          void setMessage(const QString& newMessage){ // const reference avoids useless copies
              if (m_message == newMessage) // Don't notify if the value doesn't change
                  return;
              m_message=newMessage;
              emit messageChanged(m_message) // Send the changed value further
          }
          

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

          DiracsbracketD 1 Reply Last reply
          4
          • ODБOïO ODБOï

            Hi,

            getCurrentValue is not supposed to be called when currentValueChanged is emited

            You generally use getCurrentValue from qml side to get the value of a private property (like accessor)

            class MachineBackend : public QObject
            {
                Q_OBJECT
            public:
                Q_PROPERTY(QString message READ getMessage  WRITE setMessage NOTIFY messageChanged)
            
            QString getMessage(){
            return m_message;
            }
            void setMessage(QString newMessage){
            m_message=newMessage;
            emit messageChanged() // Will notify en ensure value is refreshed
            }
            private :
             QString m_message;
            

            then in qml :

            Text{
            text : myObj.getMessage // will refresh
            text : myObj.message
            }

            GrecKoG Offline
            GrecKoG Offline
            GrecKo
            Qt Champions 2018
            wrote on last edited by GrecKo
            #5

            @LeLev said in Update Q_PROPERTY: how does the notify signal work?:

            getCurrentValue is not supposed to be called when currentValueChanged is emited

            OP doesn't have a currentValueChanged signal.

            You generally use getCurrentValue from qml side to get the value of a private property (like accessor)

            No, you generally use currentValue from qml, you shouldn't call getters directly. And it won't work in your example since getMessage is not invokable (that's a good thing).

            ODБOïO 1 Reply Last reply
            1
            • GrecKoG GrecKo

              @LeLev said in Update Q_PROPERTY: how does the notify signal work?:

              getCurrentValue is not supposed to be called when currentValueChanged is emited

              OP doesn't have a currentValueChanged signal.

              You generally use getCurrentValue from qml side to get the value of a private property (like accessor)

              No, you generally use currentValue from qml, you shouldn't call getters directly. And it won't work in your example since getMessage is not invokable (that's a good thing).

              ODБOïO Offline
              ODБOïO Offline
              ODБOï
              wrote on last edited by ODБOï
              #6

              @GrecKo this is just naming confusion,my bad

              1 Reply Last reply
              0
              • ODБOïO ODБOï

                Hi,

                getCurrentValue is not supposed to be called when currentValueChanged is emited

                You generally use getCurrentValue from qml side to get the value of a private property (like accessor)

                class MachineBackend : public QObject
                {
                    Q_OBJECT
                public:
                    Q_PROPERTY(QString message READ getMessage  WRITE setMessage NOTIFY messageChanged)
                
                QString getMessage(){
                return m_message;
                }
                void setMessage(QString newMessage){
                m_message=newMessage;
                emit messageChanged() // Will notify en ensure value is refreshed
                }
                private :
                 QString m_message;
                

                then in qml :

                Text{
                text : myObj.getMessage // will refresh
                text : myObj.message
                }

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @LeLev said in Update Q_PROPERTY: how does the notify signal work?:

                getCurrentValue is not supposed to be called when currentValueChanged is emited

                as far as I know, that is exactly what is happening, the as READ marked function is called from the QML side, as soon as you emit the currentValueChanged signal.
                But that is happening automaticaly in the background, all part of the Q_PROPERTY macro, if you want to call the getter manually, you will have to mark it as Q_INVOKABLE


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • ODБOïO Offline
                  ODБOïO Offline
                  ODБOï
                  wrote on last edited by
                  #8

                  http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html#exposing-properties

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    maxwell31
                    wrote on last edited by
                    #9

                    For clarification, I wanted to use the same notify signal for several properties

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by SGaist
                      #10

                      Then make it a separate signal and connect explicitly to that one.

                      A notify signal is associated to one property (single responsibility paradigm). Since you want a catch all signal, it should be a clear separated element. This will have the added values of making your code cleaner, clearer and avoid maintenance hell.

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

                      1 Reply Last reply
                      1
                      • dheerendraD Offline
                        dheerendraD Offline
                        dheerendra
                        Qt Champions 2022
                        wrote on last edited by
                        #11

                        In addition to what @SGaist said, if you use same signal for every properties, property binding for those properties will not work. Also you will breaking declarative flow. It will be confusing and confused code. Better avoid it.

                        Dheerendra
                        @Community Service
                        Certified Qt Specialist
                        http://www.pthinks.com

                        1 Reply Last reply
                        2
                        • SGaistS SGaist

                          Hi,

                          Some small yet useful improvements:

                          void setMessage(const QString& newMessage){ // const reference avoids useless copies
                              if (m_message == newMessage) // Don't notify if the value doesn't change
                                  return;
                              m_message=newMessage;
                              emit messageChanged(m_message) // Send the changed value further
                          }
                          
                          DiracsbracketD Offline
                          DiracsbracketD Offline
                          Diracsbracket
                          wrote on last edited by Diracsbracket
                          #12

                          @SGaist said in Update Q_PROPERTY: how does the notify signal work?:

                          emit messageChanged(m_message)

                          I often see that the notify signal of a Q_PROPERTY is defined as an argument-less void method, i.e.

                          emit messageChanged(()
                          

                          Common sense tells me that providing an argument to the method avoids an unnecessary call to the READ getter method. If that is correct, why would one define argument-less notify signals?

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            You may have a property that requires some processing to retrieve (for example slow disk/network access) and you do not want to do that for every signal émission only when there's an actual need for the data.

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

                            1 Reply Last reply
                            0
                            • GrecKoG Offline
                              GrecKoG Offline
                              GrecKo
                              Qt Champions 2018
                              wrote on last edited by
                              #14

                              And the QML engine doesn't event read the parameter of the notify signal if there's one.

                              1 Reply Last reply
                              2

                              • Login

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