采用 qmlscene 進行原型設計

Qt 5 包括 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 文件的版本。
  • 按慢運動運行所有動畫。
  • 將窗口大小重置為根項尺寸。
  • 添加導入路徑列錶。
  • 添加命名捆綁。
  • 使用翻譯文件來設置語言。

The 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 應用程序 瞭解更多信息。

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 2.3
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 2.3
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 2.3
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 2.3
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++ .