How can I avoid creating a property binding on ini

2019-02-21 14:38发布

I want to create a custom QML component with two properties one and two, which should have default values when left uninitialized. In particular, if two should get an initial value depeding on one. The following code

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

however creates a property binding: Whenever one changes, two is updated to the new value of 2 * one. How can I initialize two to the value of 2 * one without creating a binding?

标签: qt qml
3条回答
叼着烟拽天下
2楼-- · 2019-02-21 14:56

Double check that there is not need to Binding and be careful about not making codes dirty.
You can fill property with value very soon as follows:

window {
    id: win
    width: 300; height: 450
    color: "#d8d8d8"
    Item {
        property int val1
        property int val2
        property int val3: parent.width    //<-- Binding
        Component.onCompleted: {
            val1 = win.width;    //<---|
            val2 = win.height;   //<---|=== There is no binding. Just changes value
            /* ... */
        }
    }
}

(I am not sure, you may be able to set initial value using Component.onStatusChanged and Component.Ready status)

Notice for Performance: Signal and Javascript codes have some performance impact. It may be more performant to use bindings. Use Profiler to check that. If you want to set initial values of multiple property or you have already used onCompleted signal, so this will improve the performance!

查看更多
神经病院院长
3楼-- · 2019-02-21 15:05

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

查看更多
倾城 Initia
4楼-- · 2019-02-21 15:06

Explicitly break the binding upon component completion:

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

The two = two assignment breaks the binding and two is no longer updated as one changes.

查看更多
登录 后发表回答