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

后端 未结 4 832
野的像风
野的像风 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条回答
  •  Happy的楠姐
    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!

提交回复
热议问题