指定Qt Quick 代码样式
您可以使用内置的代码格式化工具(已废弃)或qmlformat 等外部工具,根据 QML 编码约定自动格式化 QML 文件。
要全局指定 QML 代码样式,请执行以下操作
- 转到首选项> > 。 Qt Quick>Code Style 。
- 在Custom settings 中,选择要修改的设置,然后选择Copy 。
- 为设置命名,然后选择OK 。
- 在Formatter Selection 中,选择要使用的格式器。
要覆盖特定项目的全局首选项,请选择Projects >Code Style 。
使用内置格式化器
内置格式器已被弃用,转而使用 qmlformat 工具或自定义格式器。
要设置内置格式器的全局首选项,请执行以下操作
- 在Formatter Selection 中,选择Built-in formatter 。
- 指定如何解释Tab键的按下,如何对齐续行,以及设置代码行的最大行长。
使用 qml 格式
为qmlformat工具设置全局首选项:
- 在Formatter Selection 中,选择QMLFormat 。
- 在Global qmlformat Configuration 中,设置代码样式。该字段使用与
.qmlformat.ini
文件相同的格式,并覆盖通用配置位置 (QStandardPaths::GenericConfigLocation) 中的.qmlformat.ini
文件。
qmlformat工具会在与被格式化文件相同的目录中搜索.qmlformat.ini
文件。如果找不到,它会搜索父目录直至根目录。如果找不到.qmlformat.ini
文件,则使用全局首选项。更多信息,请参阅qmlformat 配置设置。
注意: 如果项目目录或任何父目录中存在自定义的.qmlformat.ini
文件,它将优先于全局配置。
使用自定义工具
使用与 qmlformat 兼容的自定义工具:
- 在Formatter Selection 中,选择Custom formatter 。
- 在Command 中,输入工具的路径。
- 在Arguments 中,输入运行工具的选项。
- 查看右侧
.qmlformat.ini
文件中的代码样式集。
另请参阅 自动格式化 QML/JS 文件、缩进文本或代码、查找首选项和指定代码样式。
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.