2017-07-15 37 views
0

我有一個對象,我想旋轉一個點,其座標存儲在屬性offsetxoffsety中。這是我的目標:變換內QML對象屬性

Rectangle { 
    id: rec1 
    property int offsetx: width/2 
    property int offsety: height/2 
    height: 50 
    width: 50 
    color: "blue" 
    x: originx - offsetx 
    y: originy - offsety 
    transform: Rotation { origin.x: offsetx ; origin.y: offsety; angle: 45} 
} 

變換將既不承認也不offsetxoffsety,而position屬性X,Y,這既取決於髒性能,做工精細。如果我寫rec1.offsetx那麼轉換就可以識別它,但是如果我想要實例化這些矩形中的很多(它們不一定具有id),則使用此方法可能會出現問題。

回答

1

將上面的代碼移到單獨的qml文件中。
每個qml文件至少有一個根項目,其屬性可用於其子項而沒有明確的限定條件。
SampleRect.qml

import QtQuick 2.6 

Rectangle { 
    id: rec1 
    property int offsetx: width/2 
    property int offsety: height/2 
    height: 50 
    width: 50 
    color: "blue" 
    x: 100 - offsetx 
    y: 100 - offsety 
    transform: Rotation { origin.x: offsetx ; origin.y: offsety; angle: 45} //<- offsetx is accessible. same as rec1.offsetx 
} 

使用它main.qml文件

SampleRect { 
}