使用Qt.createComponent 动态加载组件
import QtQuick 2.0
Item {
id: container
width: 300; height: 300
function loadButton() {
// 定义一个对象 object createComponent(url, mode, parent)
var component = Qt.createComponent("HMButton.qml");
console.log(component.status)
if (component.status == Component.Ready) {
// 指定父对象
var button = component.createObject(container);
button.color = "red";
}
}
// 当加载好组件时 调用动态加载函数
Component.onCompleted: loadButton()
}