美化源代码

美化代码是指在源代码文件中应用缩进和样式。使用实验性美化插件,使用以下外部工具格式化源代码:

Beautifier 插件会将源代码解析为赋值语句、if 块、循环等组件结构,并根据 Beautifier 偏好进行格式化。你可以使用预定义的样式,也可以定义自己的样式。

保存文件时自动格式化:

  1. 下载并安装用于格式化源代码的工具:

    注意: 对于某些平台,您可能需要从源代码中构建工具。

  2. 启用 Beautifier 插件。

    注: Qt Creator 10.0.0 起,默认启用 ClangFormat 插件。请访问首选项>C++ >Formatting mode ,如果启用了 Beautifier,请选择Disable 关闭 ClangFormat,因为将它们结合使用可能会导致意想不到的结果。

  3. 转到首选项>Beautifier >General ,选择要使用的工具。

    美化器首选项中的常规选项卡

  4. 选择Automatic formatting on file save 可在保存文件时使用Tool 字段中选择的工具自动美化文件。
  5. 转到Artistic StyleClangFormatUncrustify 设置工具可执行文件的路径,并选择定义使用样式的配置文件。

    美化器偏好设置中的 "取消去壳 "选项卡

    美化器解壳首选项

格式化当前打开的文件

转到Tools >Beautifier >Artistic Style,ClangFormatUncrustify ,选择用于格式化当前文件中文本的操作。

您可以为这些操作创建键盘快捷键

转到Format Current File 来格式化当前打开的文件。

使用 ClangFormat 在光标处设置格式

当未选择文本时,转至Tools >Beautifier >ClangFormat >Format at Cursor 对光标下的语法实体进行格式化。

转到Format Line(s) 对选中的行进行格式化。

转到Disable Formatting for Selected Text ,将选定的行包入// clang-format off// clang-format on 中。

使用 Uncrustify 对选定文本进行格式化

当未选定文本时,转到Tools >Beautifier >Uncrustify >Format Selected Text ,默认情况下格式化整个文件。

要关闭此行为,请在首选项>Beautifier >Uncrustify 中清除Format entire file if no text was selected

另请参阅 启用和禁用插件美化程序

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.