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. QML property on change ?
Forum Updated to NodeBB v4.3 + New Features

QML property on change ?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
21 Posts 8 Posters 6.1k 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    Is there a way to create an event handler for when a property changes ?

    I have a property define in the QML:

    property double waterDepth: vehicle.data.totalWaterDepth
    

    vehicle.data.totalWaterDepth is somewhere in the C++, I haven't been able to locate it. What I would like to do is create a handler to mange when the value changes.

    Something like:

    onWaterDepthChanges: {
    ...
    }
    

    Kind Regards,
    Sy

    F 1 Reply Last reply
    0
    • Pradeep P NP Offline
      Pradeep P NP Offline
      Pradeep P N
      wrote on last edited by Pradeep P N
      #2

      Hi @SPlatten ,

      You should also have signals with totalWaterDepth in C++ which can help for value change updates

      Pradeep Nimbalkar.
      Upvote the answer(s) that helped you to solve the issue...
      Keep code clean.

      SPlattenS 1 Reply Last reply
      1
      • Pradeep P NP Pradeep P N

        Hi @SPlatten ,

        You should also have signals with totalWaterDepth in C++ which can help for value change updates

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #3

        @Pradeep-P-N , thank you, thats exactly the way I would do it, the trouble is I didn't code the C++ and I have no idea where the change is made, I've tried to locate it and failed.

        Kind Regards,
        Sy

        JoeCFDJ 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @Pradeep-P-N , thank you, thats exactly the way I would do it, the trouble is I didn't code the C++ and I have no idea where the change is made, I've tried to locate it and failed.

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #4

          @SPlatten Can you try?

              Connections { 
                  target: vehicle.data
                  function onTotalWaterDepthChanged() {
                      -----
                  }
              }
          

          You can set a break point at ------

          SPlattenS 2 Replies Last reply
          1
          • JoeCFDJ JoeCFD

            @SPlatten Can you try?

                Connections { 
                    target: vehicle.data
                    function onTotalWaterDepthChanged() {
                        -----
                    }
                }
            

            You can set a break point at ------

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #5

            @JoeCFD, thank you, I will at some point soon, I have other build issues now.

            Kind Regards,
            Sy

            1 Reply Last reply
            0
            • SPlattenS SPlatten

              Is there a way to create an event handler for when a property changes ?

              I have a property define in the QML:

              property double waterDepth: vehicle.data.totalWaterDepth
              

              vehicle.data.totalWaterDepth is somewhere in the C++, I haven't been able to locate it. What I would like to do is create a handler to mange when the value changes.

              Something like:

              onWaterDepthChanges: {
              ...
              }
              
              F Offline
              F Offline
              freedbrt
              wrote on last edited by freedbrt
              #6

              @SPlatten said in QML property on change ?:

              onWaterDepthChanges: {
              ...
              }

              It won't work, if totalWaterDepth doesn't have signal or signal not emitted. Only way to do this without signals, is checking property changes by the timer, but it is a bad way.

              SPlattenS 1 Reply Last reply
              1
              • JoeCFDJ JoeCFD

                @SPlatten Can you try?

                    Connections { 
                        target: vehicle.data
                        function onTotalWaterDepthChanged() {
                            -----
                        }
                    }
                

                You can set a break point at ------

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by SPlatten
                #7

                @JoeCFD , the property:

                property double waterDepth : vehicle.data.totalWaterDepth ? vehicle.data.totalWaterDepth : 0
                

                I tried the following:

                 Connections {
                      target: vehicle.data
                      function onTotalWaterDepthChanged() {
                          console.log("onTotalWaterDepthChanged: ", root.waterDepth)
                      }
                  }
                
                  onWaterDepthChanged: {
                      console.log("New water depth: ", root.waterDepth)
                  }
                

                I rebuilt and run up and when I send a new water depth, I can see it being received by another process but nothing is displayed in the console. I would have thought that the QML property would have had events such as Changed. vehicle.data.totalWaterDepth comes from the C++ application.

                Kind Regards,
                Sy

                1 Reply Last reply
                0
                • F freedbrt

                  @SPlatten said in QML property on change ?:

                  onWaterDepthChanges: {
                  ...
                  }

                  It won't work, if totalWaterDepth doesn't have signal or signal not emitted. Only way to do this without signals, is checking property changes by the timer, but it is a bad way.

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #8

                  @freedbrt , doesn't the property type itself have any events that can be implemented ?

                  Kind Regards,
                  Sy

                  D JoeCFDJ 2 Replies Last reply
                  0
                  • SPlattenS SPlatten

                    @freedbrt , doesn't the property type itself have any events that can be implemented ?

                    D Offline
                    D Offline
                    Drooke
                    wrote on last edited by
                    #9

                    @SPlatten the (assumed) Qproperty is defined in the (vehicle.data) class generally with a type, getter, setter, and Signal. If you can find in your codebase where the Qproperty is defined you can piggyback off of that signal.

                    SPlattenS 1 Reply Last reply
                    0
                    • D Drooke

                      @SPlatten the (assumed) Qproperty is defined in the (vehicle.data) class generally with a type, getter, setter, and Signal. If you can find in your codebase where the Qproperty is defined you can piggyback off of that signal.

                      SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by
                      #10

                      @Drooke , this is the biggest problem I'm having the source is huge and I've searched many times for something that could explain how it is accessed, I can see nothing, "vehicle" exists in various places, "totalWaterDepth" also exists in several places, but there nothing obvious that explains the access of vehicle.data.totalWaterDepth.

                      Kind Regards,
                      Sy

                      1 Reply Last reply
                      0
                      • SPlattenS SPlatten

                        @freedbrt , doesn't the property type itself have any events that can be implemented ?

                        JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on last edited by
                        #11

                        @SPlatten totalWaterDepthChanged() is a signal and has to be defined by the coder. If there is no reaction for your change, that means totalWaterDepthChanged() signal does not exist. You can search for it with grep.

                        1 Reply Last reply
                        0
                        • fcarneyF Offline
                          fcarneyF Offline
                          fcarney
                          wrote on last edited by
                          #12

                          Look for Q_PROPERTY macros:

                          Q_PROPERTY(int ted READ ted WRITE setted NOTIFY tedChanged)
                          

                          Note: the NOTIFY signal can be named anything.

                          C++ is a perfectly valid school of magic.

                          JoeCFDJ 1 Reply Last reply
                          1
                          • fcarneyF fcarney

                            Look for Q_PROPERTY macros:

                            Q_PROPERTY(int ted READ ted WRITE setted NOTIFY tedChanged)
                            

                            Note: the NOTIFY signal can be named anything.

                            JoeCFDJ Offline
                            JoeCFDJ Offline
                            JoeCFD
                            wrote on last edited by
                            #13

                            @fcarney is right. The signal name can be anything. I normally use tedChanged in my app.

                            1 Reply Last reply
                            0
                            • L Offline
                              L Offline
                              lemons
                              wrote on last edited by
                              #14

                              Couldn't vehicle.data also be a struct with a Q_GADGET macro?
                              → in this case there might only be a dataChanged signal, instead of a signal for each member of the struct

                              SPlattenS 1 Reply Last reply
                              0
                              • L lemons

                                Couldn't vehicle.data also be a struct with a Q_GADGET macro?
                                → in this case there might only be a dataChanged signal, instead of a signal for each member of the struct

                                SPlattenS Offline
                                SPlattenS Offline
                                SPlatten
                                wrote on last edited by
                                #15

                                @lemons , its not my code and I have no say on how it is or was coded.

                                Kind Regards,
                                Sy

                                D 1 Reply Last reply
                                0
                                • SPlattenS SPlatten

                                  @lemons , its not my code and I have no say on how it is or was coded.

                                  D Offline
                                  D Offline
                                  Drooke
                                  wrote on last edited by
                                  #16

                                  @SPlatten try console.log(vehicle) and console.log(vehicle.data) in a component.oncompleted block. which may print out the classname / QObject type. This may point to the location of your code you're looking for.

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

                                    To answer the original question :

                                    Is there a way to create an event handler for when a property changes ?

                                    Something like:

                                    property double waterDepth: vehicle.data.totalWaterDepth
                                    onWaterDepthChanges: {
                                       ...
                                    }
                                    

                                    The corresponding change signal would be waterDepthChanged, with its handler being onWaterDepthChanged: ....

                                    Scrolling through your past exchange would make me guess that vehicle has a property data which is a QQmlPropertyMap where the totalWaterDepth is eventually set. Doesn't a global search followed by some code unravelling give you any clues?

                                    But why do you need a signal handler in the first place? It's generally a code smell and should be available when possible (moreso when it's not a boolean property).

                                    SPlattenS 2 Replies Last reply
                                    0
                                    • GrecKoG GrecKo

                                      To answer the original question :

                                      Is there a way to create an event handler for when a property changes ?

                                      Something like:

                                      property double waterDepth: vehicle.data.totalWaterDepth
                                      onWaterDepthChanges: {
                                         ...
                                      }
                                      

                                      The corresponding change signal would be waterDepthChanged, with its handler being onWaterDepthChanged: ....

                                      Scrolling through your past exchange would make me guess that vehicle has a property data which is a QQmlPropertyMap where the totalWaterDepth is eventually set. Doesn't a global search followed by some code unravelling give you any clues?

                                      But why do you need a signal handler in the first place? It's generally a code smell and should be available when possible (moreso when it's not a boolean property).

                                      SPlattenS Offline
                                      SPlattenS Offline
                                      SPlatten
                                      wrote on last edited by
                                      #18

                                      @GrecKo , I tried that and it doesn't work, I think because the primitive type double is not an object.

                                      Kind Regards,
                                      Sy

                                      GrecKoG 1 Reply Last reply
                                      0
                                      • GrecKoG GrecKo

                                        To answer the original question :

                                        Is there a way to create an event handler for when a property changes ?

                                        Something like:

                                        property double waterDepth: vehicle.data.totalWaterDepth
                                        onWaterDepthChanges: {
                                           ...
                                        }
                                        

                                        The corresponding change signal would be waterDepthChanged, with its handler being onWaterDepthChanged: ....

                                        Scrolling through your past exchange would make me guess that vehicle has a property data which is a QQmlPropertyMap where the totalWaterDepth is eventually set. Doesn't a global search followed by some code unravelling give you any clues?

                                        But why do you need a signal handler in the first place? It's generally a code smell and should be available when possible (moreso when it's not a boolean property).

                                        SPlattenS Offline
                                        SPlattenS Offline
                                        SPlatten
                                        wrote on last edited by
                                        #19

                                        @GrecKo said in QML property on change ?:

                                        QQmlPropertyMap

                                        Thank you, you have given me a helpful hint in that I have now located:

                                        Q_PROPERTY (QQmlPropertyMap *data READ data CONSTANT)
                                        

                                        Kind Regards,
                                        Sy

                                        1 Reply Last reply
                                        0
                                        • SPlattenS SPlatten

                                          @GrecKo , I tried that and it doesn't work, I think because the primitive type double is not an object.

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

                                          @SPlatten said in QML property on change ?:

                                          @GrecKo , I tried that and it doesn't work, I think because the primitive type double is not an object.

                                          That doesn't prevent it from triggering change signals.

                                          Maybe your vehicle or data is changed in C++ without emitting any NOTIFY signal? if totalWaterDepth is in a QQmlPropertyMap it shouldn't be able to be changed without emitting a signal so that leaves the other two.

                                          How do you know the final value changes?

                                          SPlattenS 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