问题描述
据我所知,.qml 文件可用于定义 UI,这似乎覆盖了 ui.qml 文件中写入的任何内容.那么,ui.qml文件到底有什么用呢?
As far as I can see the .qml files can be used to define the UI, which seems to override whatever is written in ui.qml file.So, what exactly is the use of the ui.qml file?
我正在使用 Qt5 和 Qt Creator.
I'm using Qt5 with Qt Creator.
推荐答案
.ui.qml
文件的存在是为了帮助 Qt Quick Designer 解决问题.例如,普通的 QML 文件可以包含 JavaScript 表达式,但 Qt Quick Designer 很难使用这些表达式.另一方面,普通的 QML 没有那么困难,并且更接近于 .ui
文件的小部件等价物 - 一个详细说明用户界面中一组项目的文档,而不是逻辑在他们身后.
The .ui.qml
file exists to help Qt Quick Designer out. Normal QML files can contain JavaScript expressions, for example, but these are difficult for Qt Quick Designer to work with. Plain QML, on the other hand, is not as difficult, and is closer to the widgets equivalent of .ui
files - a document that details a set of items in a user interface, not so much the logic behind them.
该功能 是几年前提出的在博客上:
经典的小部件设计器是围绕声明式和命令式逻辑之间的区别而构建的.声明形式是可设计的并存储在 .ui 文件中.
在 Qml 中,很容易混合声明式代码和命令式代码.如果您向 Qml 文件添加命令式指令(影响视觉方面),它们不再是纯粹的声明性指令,并且可视化编辑器中的可视化表示将中断.可视化编辑器需要一种将可视化描述转换回文本描述的方法.对于命令式代码,这通常是不可能的,Qt Quick Designer 甚至都不会尝试.
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 文档 文档指出:
从 Qt 5.4 开始,文档也可以具有文件扩展名.ui.qml".QML 引擎像处理标准 .qml 文件一样处理这些文件,并忽略扩展名的 .ui 部分.Qt Creator 将这些文件作为 Qt Quick Designer 的 UI 表单处理.这些文件只能包含 Qt Creator 定义的 QML 语言的子集.
您可以使用 Qt Creator 向导创建文件扩展名为 .ui.qml 的 UI 表单.UI 表单包含 QML 语言的纯声明性子集.建议您在设计模式下编辑表单.但是,将项目导出为别名属性只是商业功能,因此如果您使用的是 Qt Creator 的开源版本,则必须使用编辑模式来执行此操作.Qt Creator 通过显示错误消息来强制使用受支持的 QML 功能.
不支持以下功能:
- JavaScript 块
- 函数定义
- 函数调用(qsTr 除外)
- 纯表达式以外的其他绑定
- 信号处理程序
- 根项以外的其他项中的状态
- 不是从 QQuickItem 或 Item 派生的根项目
不支持以下类型:
- 行为
- 绑定
- 画布
- 组件
- 着色器效果
- 定时器
- 变换
- 过渡
这篇关于Qt5 (QML) 中的 ui.qml 文件有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!