How to initiate a binding property updating by change another property from javascript?



  • Hello

    I have some QML sample which sets a source of image and automatically changes its width and I want the width of parent has been also updated without explicit intervention

    import QtQuick 2.3
    import QtQuick.Controls 1.3
    
    Item {
        width: 300; height: 300
        Item
        {
            id: item
            width: element.width
            height: element.height
    
            Column
            {
                id: element
                Image
                {
                    id: image
                }
                Label
                {
                    id: label
                    text: "K"
                }
            }
    
            Component.onCompleted:
            {
                console.log ("Width item: " + item.width + " element: " + element.width + " image: " + image.width + " label: " + label.width)
                image.source = "img.bmp"
                console.log ("Width item: " + item.width + " element: " + element.width + " image: " + image.width + " label: " + label.width)
            }
        }
    }
    

    Console output shows that width of image is updated, but width of column (named "element") don't. How to do it automatically updated?



  • This post is deleted!


  • You should be able to do this declaratively, by declaring either:

    width: image.width

    OR

    width: childrenRect.width

    for the "element" column.

    Note that this will "bind" the values. Binding is automatic in QML declarations. If you wish to bind from an imperative statement (ie, from within a JavaScript function) you can use:

    width = Qt.binding(function() { image.width });

    Hope this helps!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.