이 페이지에서

Qt Quick 3D - 동적 모델 생성 예제

동적 모델 생성을 보여줍니다.

이 예제는 애플리케이션 내에서 모델을 동적으로 생성하는 방법을 보여줍니다. 애플리케이션 시작 시 10개의 모델이 동적으로 생성되며, ‘+’ 및 ‘-’ 버튼을 사용하여 모델을 추가하거나 제거할 수 있습니다.

어두운 우주 공간에 흩어져 있는 42개의 다채로운 Qt 로고 모델들이 역동적인 객체 생성을 보여주고 있다

설정

스포너 노드

동적으로 생성된 모델을 담을 스포너( Node )가 필요합니다.

Node {
    id: shapeSpawner
    property real range: 300
    property var instances: []
    property int count
    ...
시작

Component.onCompleted 에서 10개의 모델을 생성하여, 예제가 시작될 때 무언가가 표시되도록 합니다.

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

동적 모델

모델 추가

씬에 새로운 항목을 추가하기 위해, 먼저 ` Qt.createComponent ` 함수를 사용하여 모델에 대한 ` Component `를 생성합니다. 그런 다음 컴포넌트의 ` createObject ` 함수를 사용하여 위치와 크기를 매개변수로 전달하며 항목을 인스턴스화합니다.

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
}
모델 제거

동적으로 생성된 모델은 인스턴스 스택에서 팝(pop)하여 제거하고 파기(destroy)하기만 하면 됩니다.

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

예제 프로젝트 @ code.qt.io

© 2026 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.