IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Qt Quick 3D - Dynamic Model Creation Example

Demonstrates dynamic model creation.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

Qt Quick 3D - Dynamic Model Creation Example▲

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.

Image non disponible

Setting Up▲

 

Spawner Node▲

We are going to need a https:// to hold the dynamically created models.

 
Sélectionnez
Node {
    id: shapeSpawner
    property real range: 300
    property var instances: []
    property int count
    ...

Startup▲

We're creating 10 models at Component.onCompleted so the example shows something at startup.

 
Sélectionnez
Component.onCompleted: {
    for (var i = 0; i < 10; ++i)
        shapeSpawner.addShape()
}

Dynamic Models▲

 

Adding Models▲

In order to add a new item to the scene, we first use the https:// function to create a https:// for our model. Then we use the component's https:// function to instantiate the item, passing in the position and scale as parameters.

 
Sélectionnez
function addShape()
{
    var xPos = (2 * Math.random() * range) - range;
    var yPos = (2 * Math.random() * range) - range;
    var zPos = (2 * Math.random() * range) - range;
    var shapeComponent = Qt.createComponent("WeirdShape.qml");
    let instance = shapeComponent.createObject(shapeSpawner,
        { "x": xPos, "y": yPos, "z": zPos, "scale": Qt.vector3d(0.25, 0.25, 0.25)});
    instances.push(instance);
    count = instances.length
}

Removing Models▲

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

 
Sélectionnez
function removeShape()
{
    if (instances.length > 0) {
        let instance = instances.pop();
        instance.destroy();
        count = instances.length
    }
}

List of Files▲

Files:

List of Files▲

  • dynamiccreation/CMakeLists.txt

  • dynamiccreation/WeirdShape.qml

  • dynamiccreation/dynamiccreation.pro

  • dynamiccreation/main.cpp

  • dynamiccreation/main.qml

  • dynamiccreation/qml.qrc

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Ce document est disponible sous la licence https:// telle que publiée par la Free Software Foundation.