2014-06-08 30 views
4

似乎在QML用戶的設計中,reparent並不是真正的「設想」,因爲即使它有可能,它也涉及到創建和更改狀態,這不便於添加到每一個項目。更好的方式來重建QML中的視覺項目

import QtQuick 1.0 

Item { 
    width: 200; height: 100 

    Rectangle { 
     id: redRect 
     width: 100; height: 100 
     color: "red" 
    } 

    Rectangle { 
     id: blueRect 
     x: redRect.width 
     width: 50; height: 50 
     color: "blue" 

     states: State { 
      name: "reparented" 
      ParentChange { target: blueRect; parent: redRect; x: 10; y: 10 } 
     } 

     MouseArea { anchors.fill: parent; onClicked: blueRect.state = "reparented" } 
    } 
} 

我想知道是否有一個更優雅的方式來重新包裝物品而不污染具有不必要狀態的物品?

回答

3

不確定如果您需要使用QtQuick 1.0,但2.0版本也可以,而且更直接。

 
import QtQuick 2.0

Item { width: 200; height: 100

Rectangle { id: redRect width: 100; height: 100 color: "red" } Rectangle { id: blueRect x: redRect.width width: 50; height: 50 color: "blue" MouseArea { anchors.fill: parent; onClicked: { blueRect.parent = redRect; blueRect.x = 10; blueRect.y = 10 } } } }

+0

'ParentChange'其實並不在自己的文件獨立的對象(與內部可見的ID,如'root')工作。這個答案也適用於這種情況。 –

相關問題