Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

qt - Is it legal to assign to property that already has binding defined in QML

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)
question from:https://stackoverflow.com/questions/65832560/is-it-legal-to-assign-to-property-that-already-has-binding-defined-in-qml

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...