0

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?

David To
  • 547
  • 5
  • 12
  • i think you should put MouseArea under thetarget element, why you want to put it under container and bind it to thetarget ? – Kunal May 22 '12 at 10:57
  • I want to make an independent component and pass an item (eg. thetarget) to its API. That component may be Item, Loader, or Rectangle...which contains a MouseArea that can be used to drag the passed item. – David To May 22 '12 at 13:37

1 Answers1

3

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
        }
    }
}
baysmith
  • 5,082
  • 1
  • 23
  • 18