【发布时间】:2016-03-04 17:37:41
【问题描述】:
我有一个 GridLayout dynamicLayout,它具有动态创建/销毁的元素。当我单击按钮 myButton 时会发生创建/销毁,为此我还提供了下面的代码。
GridLayout {
id: dynamicLayout
anchors.fill: parent
Component {
id: imageComponent
Image {
visible: true
function deleteImage() {
console.log("destroying image")
destroy()
}
}
}
}
Button {
id: myButton
visible: true
x: 200
y: 200
onClicked: {
createImageObjects();
}
}
function createImageObjects() {
if (imageComponent.status == Component.Ready)
finishCreation();
else
imageComponent.statusChanged.connect(finishCreation);
}
function finishCreation() {
if (imageComponent.status == Component.Ready) {
for (var i= 0; i < 3; i++) {
var object = imageComponent.createObject(dynamicLayout, {"width": 100, "height": 100, "source": FILE_PATH});
if (object == null) {
// Error Handling
console.log("Error creating object");
} else {
myButton.clicked.connect(object.deleteImage)
}
}
} else if (imageComponent.status == Component.Error) {
// Error Handling
console.log("Error loading component:", imageComponent.errorString());
}
}
所以我打算在单击按钮时将 3 个新图像添加到布局中,同时删除旧的 3 个图像。但是,首先创建较新的 3 个映像,然后立即销毁所有 6 个映像。 (我收到 6 条带有相同点击事件的“破坏图像”消息)
如何推迟连接到下一个点击事件?
【问题讨论】:
标签: javascript qt qml qtquick2