采用 qmlscene 进行原型设计

Qt 包括 qmlscene , a utility that loads and displays QML documents even before the application is complete. This utility also provides the following additional features that are useful while developing QML applications:

  • 在最大化窗口中查看 QML 文档。
  • 以全屏模式查看 QML 文档。
  • 使窗口透明。
  • 禁用多重采样 (抗锯齿)。
  • 不检测 .qml 文件的版本。
  • 按慢运动运行所有动画。
  • 将窗口大小重置为根项尺寸。
  • 添加导入路径列表。
  • 添加命名捆绑。
  • 使用翻译文件来设置语言。

qmlscene utility is meant to be used for testing your QML applications, and not as a launcher in a production environment. To launch a QML application in a production environment, develop a custom C++ application or bundle the QML file in a module. See 部署 QML 应用程序 for more information. When given a bare Item as root element, qmlscene will automatically create a window to show the scene. Notably, QQmlComponent::create () will not do such a thing. Therefore, when moving from a prototype developed with qmlscene to a C++ application, you need to either make sure the root element is a Window or manually create a window using QtQuick 's C++ API. On the flip side, the ability to automatically create a window gives you the option to load parts of your prototype separately with qmlscene .

To load a .qml file, run the tool and select the file to be opened, or provide the file path on the command prompt:

qmlscene myqmlfile.qml
							

要查看配置选项,运行 qmlscene 采用 -help 自变量。

添加模块导入路径

Additional module import paths can be provided using the -I 标志。例如, QML 插件范例 creates a C++ plugin identified with the namespace, TimeExample . To load the plugin, you must run qmlscene 采用 -I flag from the example's base directory:

qmlscene -I imports plugins.qml
							

This adds the current directory to the import path so that qmlscene will find the plugin in the imports 目录。

注意: By default, the current directory is included in the import search path, but modules in a namespace such as TimeExample are not found unless the path is explicitly added.

加载测试数据

Often, QML applications are prototyped with test data that is later replaced by real data sources from C++ plugins. The qmlscene utility assists in this aspect by loading test data into the application context. It looks for a directory named dummydata in the same directory as the target QML file, and loads the .qml files in that directory as QML objects and bind them to the root context as properties named after the files.

例如,以下 QML 文档引用 lottoNumbers property which does not exist within the document:

import QtQuick
ListView {
    width: 200; height: 300
    model: lottoNumbers
    delegate: Text { text: number }
}
							

If, within the document's directory, there is a dummydata directory which contains a lottoNumbers.qml 文件像这样:

import QtQuick
ListModel {
    ListElement { number: 23 }
    ListElement { number: 44 }
    ListElement { number: 78 }
}
							

Then this model would be automatically loaded into the ListView in the previous document.

Child properties are included when loaded from dummydata . The following document refers to a clock.time 特性:

import QtQuick
Text { text: clock.time }
							

The text value could be filled by a dummydata/clock.qml file with a time property in the root context:

import QtQuick
QtObject { property int time: 54321 }
							

To replace this with real data, bind the real data object to the root context in C++ using QQmlContext::setContextProperty (). This is detailed in 集成 QML 和 C++ .