【问题标题】:QML binding item issueQML 绑定项目问题
【发布时间】:2012-05-22 08:14:48
【问题描述】:

我在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 不是目标的兄弟或子级)。我已经使用了Binding,函数来返回目标,但是它们都没有用。谁能告诉我怎么了?

【问题讨论】:

  • 我认为你应该把MouseArea放在目标元素下,为什么要把它放在容器下并绑定到目标?
  • 我想制作一个独立的组件并将一个项目(例如thetarget)传递给它的API。该组件可能是 Item、Loader 或 Rectangle...,其中包含可用于拖动传递的项目的 MouseArea。

标签: qt binding qml qt-quick


【解决方案1】:

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

【讨论】:

  • 这确实是我需要的。谢谢!
猜你喜欢
  • 1970-01-01
  • 2014-06-05
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 2010-11-09
  • 2018-08-08
相关资源
最近更新 更多