0
我需要排列多個qtquick對象(圓形),以使它們自己形成一個圓。我無法找到創建對象的方式,以便在創建後可以訪問它們的屬性。目前,我正在JS for循環中創建三個不同的對象:圓,旋轉和翻譯。然後,我將旋轉和平移設置爲每個圓圈的變換組件,然後它們的臨時變量(在for循環中)超出範圍。但我希望能夠隨時更改每個圓的變換組件。有沒有辦法做到這一點?在圓中排列多個對象QML
這裏是我的JS代碼:
function drawCircles() {
var translationcomponent = Qt.createComponent("translations.qml");
var circlecomponent = Qt.createComponent("circles.qml");
var rotationcomponent = Qt.createComponent("rotations.qml");
for(var i = 0; i<5; i++) {
var circle = circlecomponent.createObject(appbase);
var translation = translationcomponent.createObject(appbase);
var rotation = rotationcomponent.createObject(appbase);
rotation.angle = 72*i;
rotation.origin.x = 25;
rotation.origin.y = 25;
translation.y = -150
circle.transform = [translation,rotation];
}
我的主QML文件:
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import "drawcircles.js" as Dcop
Rectangle {
property int originx: qmlwidth/2
property int originy: qmlheight/2
id: appbase
height: qmlheight
width: qmlwidth
color: "green"
Component.onCompleted: Dcop.drawCircles();
// below here unimportant
Rectangle {
height: 50
width: height
color: "red"
radius: width/2
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
}
Text {
id: qmlw
text: appbase.width
}
Text {
anchors.left: qmlw.right
text: appbase.height
}
}
感謝您的支持;它運作了一種魅力。我希望能夠將這些圈子移到路徑上或更改它們的數量,這是我可以用這種方法做的事情嗎?我想知道我是否爲此選擇了正確的語言,或者QML是否適用於更多靜態程序? –
^我的意思是以編程方式移動它們,而不是通過輕彈。 –
當然,你可以輕鬆做到這一點。使用model.append()將項目添加到模型或以相同的方式移除。動畫你可以使用動畫項目之一。 – folibis