指定Qt Quick 代码风格
您可以使用内置的代码格式化工具(已弃用)或外部工具(如qmlformat),根据 QML 编码规范自动格式化 QML 文件。
要全局指定 QML 代码样式:
- 转到“Preferences ” > Qt Quick >Code Style 。
- 在“Custom settings ”中,选择要修改的设置,然后选择“Copy ”。

- 为设置命名,然后选择“OK ”。
- 在“Formatter Selection ”中,选择要使用的格式化器。
要为特定项目覆盖全局首选项,请选择“Projects ”>“Code Style ”。
使用内置格式化器
内置格式化器现已弃用,建议改用 qmlformat 工具或自定义格式化器。
要为内置格式化器设置全局首选项:
- 在“Formatter Selection ”中,选择“Built-in formatter ”。
- 指定如何解释Tab键的按下操作以及如何对齐后续行,并设置代码行的最大长度。
使用 qmlformat
要设置qmlformat工具的全局首选项:
- 在Formatter Selection 中,选择QMLFormat 。

- 在Global qmlformat Configuration 中,设置代码样式。您所做的更改将覆盖通用配置位置(QStandardPaths::GenericConfigLocation )中的
.qmlformat.ini文件。 - 若要将全局
qmlformat配置文件的副本添加到项目中,请选择Deploy INI File to Current Project 。
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.