1
votes

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)
1

1 Answers

3
votes

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.