1. Dynamically create objectsThere are two ways to create objects dynamically from JavaScript:
Although dynamically created objects can be used like other objects, they do not have ids in QML. 1.1. Dynamically create components You can call its The first is the parent object of the new object. The parent object can be a graphical object (ie, of type Item) or a non-graphical object (ie, of type QtObject or C++ QObject). Only graphics objects with a graphics parent are rendered onto Sprite.qml It defines a simple QML component: import QtQuick 2.0 Rectangle { width: 80; height: 50; color: "red" } main.qml imports a componentCreation.js JavaScript file, which creates the Sprite object: import QtQuick 2.0 import "componentCreation.js" as MyScript Rectangle { id: appWindow width: 300; height: 300 Component.onCompleted: MyScript.createSpriteObjects(); } componentCreation.js var component; var sprite; function createSpriteObjects() { component = Qt.createComponent("Sprite.qml"); if (component.status == Component.Ready) finishCreation(); else component.statusChanged.connect(finishCreation); } function finishCreation() { if (component.status == Component.Ready) { sprite = component.createObject(appWindow, {x: 100, y: 100}); if (sprite == null) { console.log("Error creating object"); } } else if (component.status == Component.Error) { console.log("Error loading component:", component.errorString()); } } When using files with relative paths, the paths should be relative to the file executing It is also possible to instantiate components without blocking via the incu 1.2. Creating objects from QML stringsQML objects can be created from a QML string using the Qt.createQmlObject() function, as shown in the following example: const newObject = Qt.createQmlObject(` import QtQuick 2.0 Rectangle { color: "red" width: 20 height: 20 } `, parentItem, "myDynamicSnippet" );
If the QML string uses a relative path to the file import, the path should be relative to the file defining the parent object (the second argument to the method). When building a static QML application, the QML files are scanned to detect import dependencies. This way, all necessary plugins and resources are resolved at compile time. However, this only considers explicit import statements (those at the top of a QML file), not import statements contained in string literals. To support static builds, users need to ensure that QML files that use 2. Dynamically delete objectsIn many user interfaces, it is sufficient to set the opacity of a visual object to 0 or to move the visual object off the screen instead of removing it. However, if there are a large number of dynamically created objects, deleting unused objects may yield valuable performance benefits.
Items can be deleted using Example. application.qml import QtQuick 2.0 Item { id: container width: 500; height: 100 Component.onCompleted: { var component = Qt.createComponent("SelfDestroyingRect.qml"); for (var i=0; i<5; i++) { var object = component.createObject(container); object.x = (object.width + 10) * i; } } } SelfDestroyingRect.qml import QtQuick 2.0 Rectangle { id: rect width: 80; height: 80 color: "red" NumberAnimation on opacity { to: 0 duration: 1000 onRunningChanged: { if (!running) { console.log("Destroying...") rect.destroy(); } } } } Alternatively,
Also note that if the SelfDestroyingRect instance is created statically like this: Item { SelfDestroyingRect { // ... } } This will cause errors because an object can only be dynamically destroyed if it was dynamically created. Objects created with Qt.createQmlObject() can be destroyed similarly using destroy(): const newObject = Qt.createQmlObject(` import QtQuick 2.0 Rectangle { color: "red" width: 20 height: 20 } `, parentItem, "myDynamicSnippet" ); newObject.destroy(1000); This is the end of this article about how to create dynamic QML objects in You may also be interested in:
|
<<: How to get the dynamic number of remaining words in textarea
>>: How to remove inline styles defined by the style attribute (element.style)
echarts component official website address: https...
When using docker images, images with both REPOSI...
A jQuery plugin every day - step progress axis st...
Table of contents Official introduction to Node.j...
When to install If you use the protoc command and...
We can use the scp command of Linux (scp cannot b...
Fault description percona5.6, mysqldump full back...
Table of contents Standards for smooth animation ...
Preface This article introduces a simple BAT scri...
Table of contents 1. Introduction to gojs 2. Gojs...
1. Drop-down list example The code is as follows:...
Table of contents 1. Brief Introduction 2. setInt...
Attribute check-strictly The official document pr...
You may not have had any relevant needs for this ...
There is a medicine for regret in the world, as l...