如何进行实体的中继器?

时间:2018-04-30 23:07:10

标签: qt qml

在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];
        }
      }

    }
  }
}

哪个收益率:
enter image description here

我希望看到Index 0的其他一些挤压文本,但无济于事。

如何获得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];
}

1 个答案:

答案 0 :(得分:5)

Repeater仅适用于ItemEntity不适用。{/ p>

由于Scene3D不是您通常的快速场景,因此您需要有一个知道它的转发器/视图来实例化其中的对象。

NodeInstantiator相当于3D节点的Repeater(它会将其动态创建的对象重新显示为其父节点)。

此特定转发器的概念也与MapMapItemView一起使用。