在QML中,我想遍历一个列表并实例化一个Entity。目前,我正在使用Repeater,但似乎Entity不是Component。
我尝试了以下
import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import QtQuick.Dialogs 1.0
import QtPositioning 5.2;
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import Qt3D.Extras 2.9
import QtQuick.Scene3D 2.0
Flickable {
id: flickable;
ScrollBar.vertical: ScrollBar {
background: Rectangle {
border.width:1
border.color: 'gray';
color: "#00000000";
radius: 4;
}
}
Scene3D {
id: scene3d
anchors.margins: 1;
anchors.left: parent.left;
anchors.top: parent.top;
height: 600;
width: 600;
focus: true
onActiveFocusChanged: {
console.log('flickable.interactive',flickable.interactive);
flickable.interactive = !activeFocus;
}
aspects: ["input", "logic"]
cameraAspectRatioMode: Scene3D.AutomaticAspectRatio
Entity {
id: sceneRoot
Camera {
id: camera
projectionType: CameraLens.PerspectiveProjection
fieldOfView: 45
aspectRatio: 1 // 16/9
nearPlane : 0.1
farPlane : 1000.0
position: Qt.vector3d( 255, 255, 700.0 )
upVector: Qt.vector3d( 0.0, 1.0, 0.0 )
viewCenter: Qt.vector3d( 128, 128, 0.0 )
}
OrbitCameraController {
camera: camera
}
components: [
RenderSettings {
activeFrameGraph: ForwardRenderer {
clearColor: Qt.rgba(1, 1, 1, 1)
camera: camera
}
},
// Event Source will be set by the Qt3DQuickWindow
InputSettings { }
]
PhongMaterial {
id: material
}
Entity {
Component.onCompleted: {
console.log('Entity completed.');
}
ExtrudedTextMesh { id: ex_label; text: 'Example'; depth: 0.1}
Transform{ id: ex_label_transform; translation: Qt.vector3d(128 - 10, 128, 0); scale3D: Qt.vector3d(20,20,20); }
components: [ex_label, material, ex_label_transform];
}
Repeater {
model: [{name:'foo'}]
Entity {
Component.onCompleted: {
console.log('Entity completed.');
}
ExtrudedTextMesh { id: label; text: 'Index' + index; depth: 0.1}
Transform{ id: label_transform; translation: Qt.vector3d(128 + index*10, 128, 0); scale3D: Qt.vector3d(20,20,20); }
components: [label, material, label_transform];
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望看到的其他一些挤压文本Index 0,但无济于事。
我如何获得与a等效的Repeater实例化Entities?
Entity {
PhongAlphaMaterial { id: phong_mat_0; ambient: '#000'; alpha: model_list.length > 0 ? 0.8 : 0;}
ExtrudedTextMesh { id: label_0; text: 'Index 0'; depth: 0.1}
Transform{ id: label_transform_0; translation: Qt.vector3d(128 + 0*10, 128, 0); scale3D: Qt.vector3d(20,20,20); }
components: [label_0, phong_mat_0, label_transform_0];
}
Entity {
PhongAlphaMaterial { id: phong_mat_1; ambient: '#000'; alpha: model_list.length >1 ? 0.8 : 0;}
ExtrudedTextMesh { id: label_1; text: 'Index 1'; depth: 0.1}
Transform{ id: label_transform_1; translation: Qt.vector3d(128 + 1*10, 128, 0); scale3D: Qt.vector3d(20,20,20); }
components: [label_1, phong_mat_1, label_transform_1];
}
Entity {
PhongAlphaMaterial { id: phong_mat_2; ambient: '#000'; alpha: model_list.length >2 ? 0.8 : 0;}
ExtrudedTextMesh { id: label_2; text: 'Index 2'; depth: 0.2}
Transform{ id: label_transform_2; translation: Qt.vector3d(128 + 2*10, 128, 0); scale3D: Qt.vector3d(20,20,20); }
components: [label_2, phong_mat_2, label_transform_2];
}
Run Code Online (Sandbox Code Playgroud)
Repeater仅适用于Item,Entity而不适用于。
由于Scene3D不是通常的快速场景,因此需要有一个中继器/视图来感知它,以实例化其中的对象。
NodeInstantiator等价Repeater于3D节点(它会将其动态创建的对象重新绑定到其父对象)。
特定中继器的概念也与Map和一起使用MapItemView。
| 归档时间: |
|
| 查看次数: |
235 次 |
| 最近记录: |