用户界面文件
如果创建Qt Quick UI Prototype 项目或在Qt Creator 和Qt Design Studio 之间切换,您的项目可能包含 UI 文件 (.ui.qml)。您只能在Qt Quick Designer 或Qt Design Studio 中编辑它们。
默认情况下,Qt Creator 在Design 模式下打开 UI 文件。要使用Qt Design Studio 打开它们,请选择Open With > Qt Design Studio打开 UI 文件,请在File System 或Projects 视图的右键菜单中选择 >。
.ui.qml 文件不支持以下功能:
- JavaScript 块
- 纯表达式以外的其他绑定
- 信号处理器
- 根组件以外的其他组件中的状态
- 未从QQuickItem 派生的根组件或Item
- 引用根组件的父组件
不支持以下组件:
- 行为
- 绑定
- 画布
- 着色器效果
- 计时器
- 转换
支持的方法
Qt Creator Qt Qml 支持 QML 引擎所支持的大多数 JavaScript 函数,以及 Qt QML 方法的子集。
本节列出了可在.ui.qml 文件中使用的函数。
JavaScript 函数
根据经验,我们支持纯函数。它们只依赖于其作用域内的参数并修改参数的状态,因此在给定相同参数时总是返回相同的结果。这样就可以在不破坏.ui.qml 文件的情况下转换和重新格式化属性绑定。
支持以下 JavaScript 函数:
charAt()charCodeAt()concat()endsWith()includes()indexOf()isFinite()isNaN()lastIndexOf()substring()toExponential()toFixed()toLocaleLowerCase()toLocaleStringtoLocaleUpperCase()toLowerCase()toPrecision()toString()toUpperCase()valueOf()
此外,还支持Math 和Date 对象的所有函数。
更多信息,请参阅JavaScript 对象和函数列表。
Qt QML 方法
Qt Creator 支持颜色方法、创建特定数据类型对象的辅助方法和翻译方法。
支持以下颜色方法:
支持以下辅助方法:
- Qt.formatDate()
- Qt.formatDateTime()
- Qt.formatTime()
- Qt.matrix4x4()
- Qt.point()
- Qt.quaternion()
- Qt.rect()
- Qt.size()
- Qt.vector2d()
- Qt.vector3d()
- Qt.vector4d()
支持以下翻译方法:
- qsTr()
- qsTranslate()
- QT_TRANSLATE_NOOP()
- qsTrId()
- QT_TRID_NOOP()
- QT_TR_NOOP()
注意: 请勿在 UI 文件中混合使用翻译方法。
有关使用方法的更多信息,请参阅Qt QML 方法。
使用 UI 文件
在Design 模式下编辑 UI 文件。要在代码中使用组件,可将其导出为属性:
Item {
width: 640
height: 480
property alias button: button
Button {
anchors.centerIn: parent
id: button
text: qsTr("Press Me")
}
}属性别名可将按钮导出到使用窗体的代码中。您可以使用Navigator 中的
(Export) 按钮将组件导出为属性:

例如,在使用组件的用户界面文件中,可以使用button 属性别名来实现信号处理器。在下面的代码片段中,用户界面文件的名称是MainForm.ui.qml :
MainForm {
anchors.fill: parent
button.onClicked: messageDialog.show(qsTr("Button pressed"))
}您还可以分配属性或定义行为或转换。
要从2D 或Navigator 视图直接转到.qml 文件中某个组件的实现,请右键单击该组件并在上下文菜单中选择Go to Implementation 。
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.