指定Qt Quick 代码样式

您可以使用内置的代码格式化工具(已废弃)或qmlformat 等外部工具,根据 QML 编码约定自动格式化 QML 文件。

要全局指定 QML 代码样式,请执行以下操作

  1. 转到首选项> > 。 Qt Quick>Code Style
  2. Custom settings 中,选择要修改的设置,然后选择Copy

    Qt Quick 偏好设置中的代码风格选项卡

  3. 为设置命名,然后选择OK
  4. Formatter Selection 中,选择要使用的格式器。

要覆盖特定项目的全局首选项,请选择Projects >Code Style

使用内置格式化器

内置格式器已被弃用,转而使用 qmlformat 工具或自定义格式器。

要设置内置格式器的全局首选项,请执行以下操作

  1. Formatter Selection 中,选择Built-in formatter
  2. 指定如何解释Tab键的按下,如何对齐续行,以及设置代码行的最大行长。

使用 qml 格式

qmlformat工具设置全局首选项:

  1. Formatter Selection 中,选择QMLFormat

    Qt Quick 偏好设置中 "代码风格 "选项卡中的 qmlformatter 偏好设置

  2. Global qmlformat Configuration 中,设置代码样式。该字段使用与.qmlformat.ini 文件相同的格式,并覆盖通用配置位置 (QStandardPaths::GenericConfigLocation) 中的.qmlformat.ini 文件。

qmlformat工具会在与被格式化文件相同的目录中搜索.qmlformat.ini 文件。如果找不到,它会搜索父目录直至根目录。如果找不到.qmlformat.ini 文件,则使用全局首选项。更多信息,请参阅qmlformat 配置设置

注意: 如果项目目录或任何父目录中存在自定义的.qmlformat.ini 文件,它将优先于全局配置。

使用自定义工具

使用与 qmlformat 兼容的自定义工具:

  1. Formatter Selection 中,选择Custom formatter

    Qt Quick 偏好设置中 "代码风格 "选项卡中的自定义格式化首选项

  2. Command 中,输入工具的路径。
  3. Arguments 中,输入运行工具的选项。
  4. 查看右侧.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.