Solved Binding does not update property but Connections object does
-
There must be something I am missing about how the Qml property binding system works. The following code does not update the
Text.text
property when the notify signal for either of the propertiespersonAName
andpersonBName
are changed:Text { id: titleLabel text: "%1, %2".arg(marriageModel.personAName).arg(marriageModel.personBName) }
However, using a connection object does work:
Text { id: titleLabel function updateText() { this.text = "%1, %2".arg(marriageModel.personAName).arg(marriageModel.personBName) print(text) } Connections { target: marriageModel onPersonANameChanged: titleLabel.updateText() onPersonBNameChanged: titleLabel.updateText() } }
Surely these two cases are functionally equivalent according to the Qml spec? What are the conditions under which the
Text.text
in the first example wouldn't update whenpersonANameChanged
andpersonBNameChanged
signals are emitted, but it is updated with explicitConnections
to the signals in the second example?Thanks!
-
I figured it out. I was setting the Qt property to constant in the declaration. Apparently Qml caches the property somewhere in the Qml internals and never queries for it again.
I had misunderstood the constant property attribute to mean that it was read-only, i.e. there was no setter. In fact, constant means it never changes over the course of the application's life cycle.
So that is one additional condition where which the
Text.text
would not update when the property notify signals are emitted. -
It looks to me that the signal from marriageModel is not the same as marriageModel.personAName. Is there a signal associated with personAName and personBName?
Edit:
I just saw that there are signals. It should be updating then. What does your property code look like? -
@fcarney said in Binding does not update property but Connections object does:
It looks to me that the signal from marriageModel is not the same as marriageModel.personAName. Is there a signal associated with personAName and personBName?
Edit:
I just saw that there are signals. It should be updating then. What does your property code look like?Could you please clarify? The second example shows the notify signal names associated with the properties.
-
What is the type of
marriageModel
and ispersonAName
a property of it?Can you share the .h where you declared your properties?
-
Marriage model is a QObject and the two signals we’re added as the notify signals for the two properties. Qml automatically derived the onPersonANameChanged property name from the standard naming scheme.
These properties are automatically generated in python and I unfortunately can’t share the code as it’s too complicated. So I am asking more from a carefully theoretical perspective about the Qml binding mechanism. So far as I can tell, the example should be self explanatory.
-
text: "%1, %2".arg(marriageModel.personAName).arg(marriageModel.personBName)
This binding should update itself if the personAName or personBName notify signal is emitted.
Unless you share a minimal reproducible example we can't really help you more. -
I figured it out. I was setting the Qt property to constant in the declaration. Apparently Qml caches the property somewhere in the Qml internals and never queries for it again.
I had misunderstood the constant property attribute to mean that it was read-only, i.e. there was no setter. In fact, constant means it never changes over the course of the application's life cycle.
So that is one additional condition where which the
Text.text
would not update when the property notify signals are emitted.