QML Data Models 数据模型<一>

本文探讨了QML中的数据模型,特别是QList<QObject*>类型,它用于ListView、GridView和Repeater显示数据。QML允许通过代理(delegate)绑定到model的data roles,即使组件有自己的属性也能正确显示。此外,文章提到了QSringList作为数据模型的一种,并指出在C++中为QML提供数据模型对于大数据量或复杂处理的场景十分关键。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        在QML中,ListView、GridView、Repeater 需要数据模型提供数据,然后在显示。而如何显示则需要一个代理(delegate)来定义。模型可以是静态的,也能够动态添加删除或修改。delegate可以绑定到model的date roles 。例如:

 import Qt 4.7

 Item {
     width: 200; height: 250

     ListModel {
         id: myModel
         ListElement { type: "Dog"; age: 8 }
         ListElement { type: "Cat"; age: 5 }
     }

     Component {
         id: myDelegate
         Text { text: type + ", " + age }
     }

     ListView {
         anchors.fill: parent
         model: myModel
         delegate: myDelegate
     }
 }

myModel 有两个roles ,type和age,在delegate中Text用到了这两个roles 

Text { text: type + ", " + age }

如果Text自己有type和age属性怎么办?这时我们可以用myModel.type、myModel.age来代替type和age,否则显示的将是Text的属性(property);

要在QML中绘制3D模型的运动轨迹,可以使用Qt3D模块。以下是个简单的示例,其中包含个3D模型和其轨迹图: ``` import Qt3D.Core 2.12 import Qt3D.Render 2.12 import Qt3D.Input 2.12 import Qt3D.Extras 2.12 Entity { id: sceneRoot // 3D model Entity { id: modelEntity components: [ // transform component to move the model Transform { id: modelTransform translation: Qt.vector3d(0, 0, -5) }, // mesh component with a 3D model file Mesh { source: "models/myModel.obj" } ] } // trajectory line Entity { id: trajectoryEntity // array of points for the line property vector3d[] points: [ Qt.vector3d(0, 0, -5), Qt.vector3d(1, 2, -7), Qt.vector3d(-3, 1, -9), Qt.vector3d(2, -1, -11) ] // component to draw the line LineRenderer { id: lineRenderer material: DefaultMaterial { lineWidth: 3 } primitiveType: LineRenderer.Lines geometry: Geometry { id: lineGeometry attributes: [ // position attribute Attribute { name: defaultAttributeNames.positionAttributeName attributeType: Attribute.VertexAttribute vertexBaseType: Attribute.Float vertexSize: 3 byteOffset: 0 byteStride: 12 count: trajectoryEntity.points.length buffer: Buffer { id: lineVertexBuffer type: Buffer.VertexBuffer data: trajectoryEntity.points } } ] } } } // animation to move the model along the trajectory SequentialAnimation { id: animation NumberAnimation { target: modelTransform property: "translation.x" from: 0 to: trajectoryEntity.points[0].x duration: 1000 } NumberAnimation { target: modelTransform property: "translation.y" from: 0 to: trajectoryEntity.points[0].y duration: 1000 } NumberAnimation { target: modelTransform property: "translation.z" from: 0 to: trajectoryEntity.points[0].z duration: 1000 } // loop the animation through all the trajectory points onStopped: { var nextPoint = (animation.current + 1) % trajectoryEntity.points.length; animation.to = Qt.vector3d( trajectoryEntity.points[nextPoint].x, trajectoryEntity.points[nextPoint].y, trajectoryEntity.points[nextPoint].z ); animation.start(); } } // start the animation Component.onCompleted: { animation.to = Qt.vector3d( trajectoryEntity.points[1].x, trajectoryEntity.points[1].y, trajectoryEntity.points[1].z ); animation.start(); } } ``` 在此示例中,我们定义了个3D场景,其中包含个3D模型和个轨迹图线。我们还创建了个动画,该动画将模型沿着轨迹线移动,并在达到每个点时更新目标位置以循环动画。要绘制轨迹,我们使用了LineRenderer组件,该组件根据组点在3D场景中绘制线条。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值