QML property on change ?
-
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?
-
@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?