我正在使用JS创建对象的动态实例,并尝试在创建时设置属性。我先用宽度试了一下。
var component;
var gauge;
function createVerticalGauge() {
component = Qt.createComponent("VerticalBarGauge.qml");
if (component.status == Component.Ready)
finishCreation();
else
component.statusChanged.connect(finishCreation);
}
function finishCreation() {
if (component.status == Component.Ready) {
gauge = component.createObject(root, {"x": 100, "y": 100});
gauge.width = 300;
if (gauge == null) {
// Error Handling
console.log("Error creating object");
}
} else if (component.status == Component.Error) {
// Error Handling
console.log("Error loading component:", component.errorString());
}
}
但是javascript没有改变属性。编译时不会出现错误或警告。
这是由javascript:
创建的Object的QMLimport QtQuick 2.8
import QtGraphicalEffects 1.0
import QtQuick.Controls 2.1
import QtQuick.Controls.Styles 1.4
import QtQuick.Extras 1.4
Item {
Rectangle {
id: rev
width: 100
height: 80
color: "transparent"
anchors.left: parent.left
anchors.top: parent.top
Gauge {
id: revgauge
anchors.fill: parent
anchors.margins: 10
orientation : Qt.Horizontal
tickmarkStepSize : 5000
minimumValue: 0
maximumValue: 10000
//value: Dashboard.revs
Behavior on value {
NumberAnimation {
duration: 5
}
}
Text {
font.pixelSize: (parent.height / 3)
anchors.top : parent.top
font.bold: true
font.family: "Eurostile"
color: "white"
anchors.horizontalCenter: parent.horizontalCenter
}
style: GaugeStyle {
valueBar: Rectangle {
implicitWidth: rev.height /3
color: Qt.rgba(revgauge.value / revgauge.maximumValue, 0, 1 - revgauge.value / revgauge.maximumValue, 1)
}
}
}
}
}
如何在创建对象后更改对象的属性。例如。我想改变对象的位置或颜色。