我试图根据用户与QML中的复选框的交互来动态创建并销毁组件。选中复选框,创建组件。关闭复选框,销毁组件。创建该组件有效,但销毁该组件无效。该组件仍然存在。
在QT文档的文档在这里提及的是,销毁方法应该创建一个组件后工作。有什么想法我可能在这里做错了吗?代码如下。
import QtQuick 2.7
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.1
Rectangle {
property var deficiencyType
width: 600
height: 800
color:"white"
CheckBox {
id:checkbox
text: "Check deficiency on/off"
onClicked: {
if (checked) {
deficiencyType = Qt.createComponent("Form.qml")
deficiencyType.createObject(columnRef)
} else {
deficiencyType.destroy()
console.log(deficiencyType)
}
}
}
ColumnLayout {
id:columnRef
Layout.fillHeight: true
Layout.fillWidth: true
anchors {
top: checkbox.bottom
topMargin: 10
}
}
}
正如@Alaenix所说,Loader可能会为您工作。但是要直接回答您的问题,destroy()的问题在于您要破坏的是组件,而不是所创建的对象。因此,更改此代码:
deficiencyType = Qt.createComponent("Form.qml")
deficiencyType.createObject(columnRef)
对此:
var component = Qt.createComponent("Form.qml")
deficiencyType = component.createObject(columnRef)
应该可以解决您的问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句