” Qt.binding by Binding

QML is a great tool. But few people know how to use it correctly. In this short note, I want to offer you an unspoken rule that will make your code safer for you. After all, it is so easy to shoot yourself in the foot with QML.

The rule is this: you shouldn’t write like this!

property var varOfRectangle: item.varOfItem

It should be like this!

readonly property var varOfRectangle: item.varOfItem

Or like this!

property var varOfRectangle

That is, we either necessarily put the variable in readonlyor we do not assign variable nothing at alland bind through Binding.

Item {
  id: item
  property var varOfItem
}

Rectangle {
  id: rectangle
  property var varOfRectangle
}

Binding {
  target: rectangle
  property: "varOfRectangle"
  value: item.varOfItem
}

Did I say the word “assignment”? Exactly! That's the problem! The ” operator:” does the same as if you assigned the binding via the “=” operator.

Component.onCompleted: {
  varOfRectangle = Qt.binding( function () { return item.varOfItem } )
}

And this means that your variable varOfRectangle can be overwritten with another value, and it will no longer be bound to anything.

onSomeVarChanged: {
  ...
  parent.varOfRectangle = true
  ...
}

And so you vigorously change the value of the varOfItem variable, in full confidence that it is bound to varOfRectangle, but in fact it is not.

And now about something more complex. About the QML concept, about the ideology, about the paradigm as a whole.

The essence of QML assumes that there is no time in the program, no past, no future, and therefore there is no real there is no order of sequence.

Knowing everything written above, let's return to where we started, for example, how not to write.

property var varOfRectangle: item.varOfItem

Let's look at this line again. It says that the variable can be both read and written. It is logical to assume that after reading the variable varOfRectangle we will get the value of the variable varOfItemIs this true? Can we guarantee which will happen first: reading or writing? Let me remind you that after writing to this variable, it will no longer be bound. varOfItemas we might think when reading this code.

And here we return again to the key concept of QML: the program has no time. We cannot predict what will happen first, reading or writing. The Qt library handles events in any orderin different conditions in different ways, as it considers most optimal.

Your QML code should not depend on the sequence of events.

When using Bindingyou can assign a different value to the variable at any time – the binding will not be lost and will remain working.

Summary: use readonly when declaring a variable; if you intend to change its value via the “=” operator, but want to bind it to something, use Binding. This component is controlled, it can be made active or inactive conditionally. This will prevent your code from unexpected behavior.

By violating this rule, the program becomes dependent on the read-write order, and this is a violation of the QML ideology and with 100% probability will become that very rope in your code, the length of which is enough to shoot yourself in the foot.

All the best, and write in the comments what you think about this.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *