Qt5(QML)中的ui.qml文件有什么用?

What is the use of the ui.qml files in Qt5 (QML)?

据我所知,.qml 文件可用于定义 UI,这似乎覆盖了 ui.qml 文件中的任何内容。 那么,ui.qml文件到底有什么用呢?

我正在使用 Qt5 和 Qt Creator。

.ui.qml 文件的存在是为了帮助 Qt Quick Designer。例如,普通 QML 文件可以包含 JavaScript 表达式,但 Qt Quick Designer 很难使用这些表达式。另一方面,普通 QML 并不那么困难,并且更接近于等同于 .ui 文件的小部件 - 一种详细说明用户界面中的一组项目的文档,而不是它们背后的逻辑。

特征was proposed several years ago on the blog

The classical Widget Designer is built around the distinction between declarative form and imperative logic. The declarative form is designable and stored in .ui files.

In Qml it is easy to mix declarative code and imperative code. If you add imperative instructions (affecting visual aspects) to your Qml files they are not purely declarative anymore and the visual representation in the visual editor will break. The visual editor needs a way to translate the visual description back into the text description. For imperative code this is not possible in general and the Qt Quick Designer does not even try.

QML Documents 文档指出:

Since Qt 5.4, a document can also have the file extension ".ui.qml". The QML engine handles these files like standard .qml files and ignores the .ui part of the extension. Qt Creator handles those files as UI forms for the Qt Quick Designer. The files can contain only a subset of the QML language that is defined by Qt Creator.

Qt Quick UI Forms:

You can use Qt Creator wizards to create UI forms that have the filename extension .ui.qml. The UI forms contain a purely declarative subset of the QML language. It is recommended that you edit the forms in the Design mode. However, exporting items as alias properties is a commercial only feature, and therefore you must use the Edit mode to do it if you are using the open source version of Qt Creator. Qt Creator enforces the use of the supported QML features by displaying error messages.

The following features are not supported:

  • JavaScript blocks
  • Function definitions
  • Function calls (except qsTr)
  • Other bindings than pure expressions
  • Signal handlers
  • States in other items than the root item
  • Root items that are not derived from QQuickItem or Item

The following types are not supported:

  • Behavior
  • Binding
  • Canvas
  • Component
  • Shader Effect
  • Timer
  • Transform
  • Transition