Qml component Concept

Info Thread
By -
0

 QML Debug - Visual Studio Marketplace

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:
  1. qmlDescribe the statement for the qml component;
  2. parentTo attach to that parent control;
  3. 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:
  1. urlIt is a qml file (you can also load a network qml file);
  2. modeMode for creating qml (synchronous or asynchronous);
  3. parentDesignate a control as the parent control;
  4. 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});
Tags:

Post a Comment

0Comments

Post a Comment (0)