Introduce component construction, destruction, and dynamic loading knowledge.
Objects/Components all have C++-like constructors and destructors
onCompletedObject construction completes automatic execution;onDestructionExecuted automatically before the object is destroyed.
QtObject {
Component.onCompleted: console.log("Completed")
Component.onDestruction: console.log("Destruction")
}
Load components using Loader
ComponentMust have a subspace to instantiate;onProgressChangedThe loading progress of the component can be obtained.
Component {
id: component
Text {
text: "Component"
}
}
Loader {
sourceComponent: component
onProgressChanged: console.log(progress)
}
Dynamically load Qml components (1)
- prototype:
qmlDescribe the statement for the qml component;parentTo attach to that parent control;filepathError reporting during loading for redirection to file
object createQmlObject(qml,
object parent,
string filepath)
- example
var object = Qt.createQmlObject('import QtQuick 2.0; Rectangle {color: "blue"; width: 50; height: 50}',
root,
"error.txt");
Dynamically load Qml components (2)
- prototype:
urlIt is a qml file (you can also load a network qml file);modeMode for creating qml (synchronous or asynchronous);parentDesignate a control as the parent control;objectas an additional attribute.
object createComponent(url, mode, parent)
object createObject(parent, object properties)
- Example:
var component = Qt.createComponent("MyComponent.qml");
if (component.status == Component.Ready)
component.createObject(parent, {x: 100, y: 100});