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

后端 未结 4 803
野的像风
野的像风 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:16

    In fact, you just shouldn't. Binding is the base behaviour of QML, if you try to avoid it, then that's because you're not thinking the good way.

    For exemple, if property two initial value is calculated with property one initial value but not property one value,

    Then that mean you want to bind on Initial value not value, you should create a readonly property which value will be property one initial value :

    readonly property int initialOne : 1;
    property int one : initialOne;
    property int two : 2 * initialOne;
    

    It could seem a little heavy, but if you think about it, the initial value is what you wanna use, and so, the concept of the property is what you really want

提交回复
热议问题