How can I avoid creating a property binding on initialization in QML?

后端 未结 4 798
野的像风
野的像风 2021-01-17 11:36

I want to create a custom QML component with two properties one and two, which should have default values when left uninitialized. In particular, i

4条回答
  •  时光说笑
    2021-01-17 12:00

    A way to explicitly tell that you don't want a binding is to call an assignment in an expression block:

    Rectangle {
      property int one: 1
      property int two: {two = 2 * one}
    }
    

    Unlike the approach of breaking the binding in onCompleted, the expression block avoids the creation and then destruction of a binding object, and it looks cleaner.

提交回复
热议问题