加载占位符数据
Qt Quick Designer 支持视图、模型和委托,因此在添加网格视图、列表视图或路径视图组件时,会自动添加ListModel 和委托组件。
然而,应用程序上下文的缺失带来了挑战。用 C++ 定义的特定模型是最明显的例子。通常情况下,上下文缺少简单的属性,这些属性要么定义在 C++ 中,要么定义在其他组件文件中。一个典型的例子是一个组件使用其父组件的属性,如parent.width 。
使用虚拟模型
如果在2D 视图中打开一个引用 C++ 模型的文件,你将什么也看不到。如果从互联网上获取模型中的数据,则无法对其进行控制。要获得可靠的数据,请使用虚拟数据。
例如,下面的代码片段描述了包含ListView 的文件example.qml ,该文件反过来指定了一个 C++ 模型:
ListView { model: dataModel delegate: ContactDelegate { name: name } }
在项目根目录下创建名为dummydata的目录,这样就不会部署到设备上。在dummydata 目录中,创建一个与model 的值同名的文件 (.qml) :
qml/exampleapp/example.qml dummydata/dataModel.qml
然后创建包含哑数据的dataModel.qml 文件:
import QtQuick 2.0 ListModel { ListElement { name: "Ariane" } ListElement { name: "Bella" } ListElement { name: "Corinna" } }
创建虚拟上下文
下面的示例介绍了一种常见模式:
Item { width: parent.width height: parent.height }
这对应用程序非常有效,但2D 视图显示的组件大小为零。由于缺少上下文,打开的文件不存在父文件。为了解决上下文缺失的问题,我们引入了虚拟上下文的概念。如果在dummydata/context 目录中放置一个与应用程序同名的文件(此处为example.qml ),就可以伪造一个父上下文:
import QtQuick 2.0 import QmlDesigner 1.0 DummyContextObject { parent: Item { width: 640 height: 300 } }
另请参阅 如何设计 UI、 UI:设计Qt Quick UI、Qt Quick UI 设计和设计Qt Quick UI。
Copyright © The Qt Company Ltd. and other contributors. 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.