Search code examples
qtbindingqmlqt-quick

QML binding item issue


I have problem in binding item in QML, for example:

Rectangle{
    id: thetarget
    width:100
    height:100
}
Item{
    id: container
    MouseArea{            
        id:mousearea
        drag.target: thetarget  //not work        
        anchors.fill: thetarget  //not work
        property int foo: thetarget.width  //work
    }
}

What I want is to make the bindings for drag.target, anchors.fill work without changing the structure (mousearea is not the sibling or child of thetarget). I have used Binding, function to return thetarget, but they are all useless. Could someone tell me what's wrong?


Solution

  • Set the parent of the mousearea to thetarget.

    import QtQuick 1.1
    
    Item {
        Rectangle {
            id: thetarget
            width: 100
            height: 100
        }
        Item {
            id: container
            MouseArea {
                id: mousearea
                parent: thetarget
                drag.target: thetarget
                anchors.fill: thetarget
                property int foo: thetarget.width
            }
        }
    }