Introduce component construction, destruction, and dynamic loading knowledge.
Objects/Components all have C++-like constructors and destructors
onCompleted
Object construction completes automatic execution;onDestruction
Executed automatically before the object is destroyed.
QtObject {
Component.onCompleted: console.log("Completed")
Component.onDestruction: console.log("Destruction")
}
Load components using Loader
Component
Must have a subspace to instantiate;onProgressChanged
The 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:
qml
Describe the statement for the qml component;parent
To attach to that parent control;filepath
Error 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:
url
It is a qml file (you can also load a network qml file);mode
Mode for creating qml (synchronous or asynchronous);parent
Designate a control as the parent control;object
as 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});