QML property on change ?
-
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: { ... }
-
Hi @SPlatten ,
You should also have
signals
withtotalWaterDepth
in C++ which can help for value change updates -
Hi @SPlatten ,
You should also have
signals
withtotalWaterDepth
in C++ which can help for value change updates@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.
-
@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.
-
@SPlatten Can you try?
Connections { target: vehicle.data function onTotalWaterDepthChanged() { ----- } }
You can set a break point at ------
-
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: { ... }
@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.
-
@SPlatten Can you try?
Connections { target: vehicle.data function onTotalWaterDepthChanged() { ----- } }
You can set a break point at ------
@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.
-
@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.
-
@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.
@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.
-
Look for Q_PROPERTY macros:
Q_PROPERTY(int ted READ ted WRITE setted NOTIFY tedChanged)
Note: the NOTIFY signal can be named anything.
-
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 -
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 beingonWaterDepthChanged: ...
.Scrolling through your past exchange would make me guess that
vehicle
has a propertydata
which is aQQmlPropertyMap
where thetotalWaterDepth
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).
-
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 beingonWaterDepthChanged: ...
.Scrolling through your past exchange would make me guess that
vehicle
has a propertydata
which is aQQmlPropertyMap
where thetotalWaterDepth
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).
-
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 beingonWaterDepthChanged: ...
.Scrolling through your past exchange would make me guess that
vehicle
has a propertydata
which is aQQmlPropertyMap
where thetotalWaterDepth
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).
-
@GrecKo , I tried that and it doesn't work, I think because the primitive type double is not an object.
@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?