QML绑定项目问题

时间:2012-05-22 08:14:48

标签: qt binding qml qt-quick

我在QML中绑定项目时遇到问题,例如:

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
    }
}

我想要的是使drag.target,anchors.fill的绑定在不改变结构的情况下工作(mousearea不是thetarget的兄弟或子节点)。我使用了Binding,函数来返回目标,但它们都没用。有人能告诉我什么是错的吗?

1 个答案:

答案 0 :(得分:3)

mousearea的父级设为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
        }
    }
}