Qt Quick 3D - Dynamic Model Creation Example

Demonstrates dynamic model creation.

This example demonstrates creating models dynamically in an application. 10 models are created dynamically at the start of the application, and more can be added or removed using the Add Model and Remove Model buttons. Dynamic model count is limited to 100 in this example, but that is in no way a limitation of QtQuick3D.

Setting Up

Spawner Node

We are going to need a Node to hold the dynamically created models.

Node {
    id: shapeSpawner
    property real range: 300
    property var instances: []
    readonly property int maxInstances: 100

    function addOrRemove(add) {

We're creating 10 models at Component.onCompleted to avoid opening into an empty view.

Component.onCompleted: {
    // Create 10 instances to get started
    for (var i = 0; i < 10; ++i)

Dynamic Models

The addOrRemove() function called during startup can add or remove the models.

Adding Models

A model component is created using the Qt.createComponent function, and instantiated with position and scale parameters using the createObject function.

// Create a new weirdShape at random postion
var xPos = (2 * Math.random() * range) - range;
var yPos = (2 * Math.random() * range) - range;
var zPos = (2 * Math.random() * range) - range;
var weirdShapeComponent = Qt.createComponent("WeirdShape.qml");
let instance = weirdShapeComponent.createObject(
        shapeSpawner, { "x": xPos, "y": yPos, "z": zPos,
            "scale": Qt.vector3d(0.25, 0.25, 0.25)});
Removing Models

Dynamically created models are removed simply by popping and destroying them from the instance stack.

// Remove last item in instances list
let instance = instances.pop();


© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.