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

后端 未结 4 801
野的像风
野的像风 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.

    0 讨论(0)
  • 2021-01-17 12:04

    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!

    0 讨论(0)
  • 2021-01-17 12:16

    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.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题