2012-05-22 81 views
0

我有問題在QML結合的項目,例如: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工作在不改變結構(鼠標區域是不是目標的兄弟姐妹或孩子)。我用Binding函數返回目標,但它們都沒用。有人能告訴我什麼是錯的嗎?

+0

我認爲你應該把MouseArea放在target元素下,爲什麼你想把它放在容器下並將它綁定到target? – Kunal

+0

我想製作一個獨立的組件並將一個項目(例如.target)傳遞給它的API。該組件可能是Item,Loader或Rectangle ...,它包含一個MouseArea,可用於拖動傳遞的項目。 –

回答

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

這真的是我需要的。謝謝! –