Qt Quick 3D - 동적 모델 생성 예제
동적 모델 생성을 시연합니다.
이 예는 애플리케이션에서 동적으로 모델을 만드는 방법을 보여줍니다. 애플리케이션 시작 시 10개의 모델이 동적으로 생성되며 + 및 - 버튼을 사용하여 더 많은 모델을 추가하거나 제거할 수 있습니다.
설정
스포너 노드
동적으로 생성된 모델을 보관할 Node 노드가 필요합니다.
시작
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 }
모델 제거하기
동적으로 생성된 모델은 인스턴스 스택에서 팝업 및 소멸하여 제거할 수 있습니다.
function removeShape() { if (instances.length > 0) { let instance = instances.pop(); instance.destroy(); count = instances.length } }
© 2025 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.