1

Let's say property 'x' has an immidiate value in QML, and another property 'y' is bound to 'x'

Text { id: mytext;  x: 100;  y: x;  text: x+","+y; }

Is it legal to assign another value to any of the properties? i.e. is binding 'y: x' simply deleted/overriden, or it enters some sort of unsupported/erroneous state?

Button { onClicked: { mytext.x = 120; mytext.y = 130; } }

What if I specify another binding explicitly in the same QML file:

Binding { target: mytext; property: "x"; value: 140; }
Binding on y { value: 150; } //insert this line into mytext

Is it different if "when" is specified? Can I specify multiple conditional bindings?

Binding { target: mytext; property: "x"; value: 160; when: width < 600; }
Binding { target: mytext; property: "x"; value: 170; when: width > 800; }

Is Animation or Behavior any different?

NumberAnimation on x { from: 100; to: 200; duration: 500 } //insert line into mytext
NumberAnimation { target: mytext; property: "y"; from: 100; to: 200; duration: 500 }

Is assigning (or creating a binding) from C++ code any different?

mytextPointer->setX(190);

Please note that I'm not asking if this works on your system - instead I'd like to know if this code supposed to work as intended.

My intention is:

  • "directly specified" values (x=100 and y=x) are default,
  • whenever they are assigned, their value is replaced with whatever was assigned

My current undestanding of documentation is:

  • having multiple conditional bindings is ok (as long as both conditions cannot be true at the same time?) - the binding restores previous value (not necessarily default) after condition is no longer true;
  • animation/behavior/transition overrides the binding when it is running/active, but does not restore previous value(?) after finishing;
  • everything else is unclear (but apparently works; and if multiple bindings are assigned then evidently the first one provides the value)
Jack White
  • 896
  • 5
  • 7

1 Answers1

3

That's a lot of questions packed into one question. I'll focus on your stated intentions:

  • "directly specified" values (x=100 and y=x) are default,

  • whenever they are assigned, their value is replaced with whatever was assigned

If you have a binding, like so:

Text { id: mytext;  x: 100;  y: x;  text: x+","+y; }

And you later change the value of x, under the hood it should be emitting an xChanged signal. The binding means y will be listening for that signal and update accordingly.

Now if you later change y, like so:

Button { onClicked: { mytext.y = 130; } }

The binding is broken. y will no longer listen for changes from x.

For animations, if you tell y to animate to a value different from x, then the binding is broken. Intermediate animation steps do not break the binding though. You can have y still bound to x, but as x changes, y can animate those changes and remain bound.

JarMan
  • 7,589
  • 1
  • 10
  • 25
  • Thank you for your nice answer. I assume "broken" means "cleanly deleted", so the answer to my question is then: it is legal to do write such code - QML is not supposed to crash, exhibit undefined behavior, print errors to log, etc solely because a binding is broken in such a way (which is what I expected). I also assume it means that some code (like in conditional Binding element) may choose to somehow "preserve" and later "restore" the binding (doesn't matter how) - but if it doesn't, the binding is completely gone and forgotten as soon as it is broken. It that correct? – Jack White Jan 22 '21 at 06:57
  • 1
    1. Broken means disconnected. `y` is no longer connected to the `onXChanged` signal. 2. Yes, It is legal and common to write code like that. 3. A `Binding` object is able to restore bindings that become disconnected. – JarMan Jan 22 '21 at 13:05