介绍组件构造,销毁,动态加载小知识。
onCompleted
对象构造完成自动执行;onDestruction
对象销毁前自动执行。QtObject {
Component.onCompleted: console.log("Completed")
Component.onDestruction: console.log("Destruction")
}
Component
必须具有子空间才能实例化;onProgressChanged
可以获取到组件的加载进度。Component {
id: component
Text {
text: "Component"
}
}
Loader {
sourceComponent: component
onProgressChanged: console.log(progress)
}
qml
为qml组件描述语句;parent
为依附于那个父类控件;filepath
为重定向到文件的加载过程中的错误报告object createQmlObject(qml,
object parent,
string filepath)
var object = Qt.createQmlObject('import QtQuick 2.0; Rectangle {color: "blue"; width: 50; height: 50}',
root,
"error.txt");
url
为qml文件(还可以加载网络qml文件);mode
为创建qml的模式(同步或异步);parent
指定某个控件为父类控件;object
为附加属性。object createComponent(url, mode, parent)
object createObject(parent, object properties)
var component = Qt.createComponent("MyComponent.qml");
if (component.status == Component.Ready)
component.createObject(parent, {x: 100, y: 100});