Qt Creator使用clang-format实现源代码格式化排版(Windows/macOS2)

简介: Qt Creator使用clang-format实现源代码格式化排版(Windows/macOS2)

重点来了,在Value里,输入以下部分;


# 编程语言: None, Cpp, Java, JavaScript, ObjC, Proto, TableGen, TextProto
Language: Cpp
# 基础样式
BasedOnStyle: Google
#指针的*的挨着哪边,例如int* a
DerivePointerAlignment: false
PointerAlignment: Left
# 访问修饰符前的空格,例如public,private等
AccessModifierOffset: -4
# 缩进宽度
IndentWidth: 4
# 要保留的最大连续空行数
MaxEmptyLinesToKeep: 1
# 大括号{}的换行方式,数值是Allman或Attach
BreakBeforeBraces: Allman
# 是否允许短方法单行,例如int f() { return 0; }
AllowShortFunctionsOnASingleLine: true
# 支持一行的if,例如值为true,则可以放在一行上。if (a) return;
AllowShortIfStatementsOnASingleLine: false
# 在未封闭(括号的开始和结束不在同一行)的括号中的代码是否对齐,为true,则将参数在左方括号后水平对齐
AlignAfterOpenBracket: true
# switch的case缩进
IndentCaseLabels: false
# 针对OC的block的缩进宽度
ObjCBlockIndentWidth: 4
# 针对OC,属性名后加空格
ObjCSpaceAfterProperty: true
# 每行字符的长度
ColumnLimit: 0
# 注释对齐
AlignTrailingComments: true
# 括号后加空格,例如(int) i;
SpaceAfterCStyleCast: false
# 换行的时候对齐操作符
AlignOperands: true
# 中括号两边空格 []
SpacesInSquareBrackets: false
# 多行声明语句按照=对齐
AlignConsecutiveDeclarations: false
# 容器类的空格 例如 OC的字典
SpacesInContainerLiterals: false
# 在构造函数初始化时按逗号断行,并以冒号对齐
BreakConstructorInitializersBeforeComma: true
# 函数参数换行
AllowAllParametersOfDeclarationOnNextLine: true
#在续行(#下一行)时的缩进长度
ContinuationIndentWidth: 4
# tab键盘的宽度
TabWidth: 4
# 赋值运算符前加空格
SpaceBeforeAssignmentOperators: true
# 行尾的注释前加1个空格
SpacesBeforeTrailingComments: 1

点击确认按钮之后,最终会在路径C:\Users\<用户名>\AppData\Roaming\QtProject\qtcreator\beautifier\clangformat\myclangconfig生成.clang-format文件。如果该文件使用了中文注释,那么就需要手动另存为UTF-8的编码,否则格式化时会报错:error: Got empty plain scalar。


还有一个更好的办法,先进入路径C:\Users\<用户名>\AppData\Roaming\QtProject\qtcreator\beautifier\clangformat\myclangconfig,然后在命令行终端输入以下命令,可以生成参数模板.clang-format文件

clang-format-2663a25f.exe -style=Microsoft -dump-config > .clang-format


然后再该文件的基础上,修改自己想要的参数。比如:

BasedOnStyle: Microsoft
AccessModifierOffset: -4
AlignConsecutiveMacros: true
AlignTrailingComments: true
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: false
BreakBeforeBraces: Allman
ColumnLimit: 0


详细的变量说明请参见clang官网https://clang.llvm.org/docs/ClangFormatStyleOptions.html

也可以看看效果 https://clangformat.com/

Google C++ Style Guide https://google.github.io/styleguide/cppguide.html

选择Tools->Options,点击左栏的Beautifier,选择General;勾选Enable auto format on save file;Tool选择ClangFormat。


image.png


通过菜单操作可以实现格式化


image.png


4、设置clang-format快捷键(可有可无)


点击Tools->Options->Environment->Keyboard;

在Filer里输入clang,滤掉一部分方便查找,然后想要的出现了;

选上FormatFile,点下那一栏就行了;

在Key sequence里手动打入Ctrl+shift+k,当然点击右边的Record按钮,按钮会变成stop recording,再在键盘上按照Ctrl->shift->k的顺序按下去,前面两个键按住了不要松,等到按到k再松开,然后快捷键栏里面会出现Ctrl+shift+k,点击stop recording,点击保存。


再补充macOS的用法


官网下载Pre-Built Binaries中macOS版本的tar-xz文件,大小300+M,注:点击macOS而不是后面的(.sig)

       clang+llvm-9.0.1-x86_64-apple-darwin.tar.xz


解压

把/clang+llvm-9.0.1-x86_64-apple-darwin/bin目录下的clang-format文件和clang-tidy复制到某个文件夹里,比如

       /Users/<username>/Qt5.12.7/Qt Creator.app/Contents/Resources/libexec/clang/bin9/clang-tidy

       /Users/<username>/Qt5.12.7/Qt Creator.app/Contents/Resources/libexec/clang/bin9/clang-format


这里的bin9是新建的文件夹,区别于原来的bin


设置QtCreator,clang-format和clang-tidy分别指向该路径

配置clang-format的格式化风格,.clang-format文件最终会保存在:

      /Users/<username>/.config/QtProject/qtcreator/beautifier/clangformat



姊妹篇


《VS2019使用clang-format实现源代码格式化排版》


https://libaineu2004.blog.csdn.net/article/details/112760035





---


参考文献

https://www.jianshu.com/p/e8e0158258f0 clang-format常用配置


https://www.jianshu.com/p/ba7aa6a2e4d1 Clang-Format在 Mac上的安装与使用


https://blog.csdn.net/core571/article/details/82867932  C++ 代码格式化方法(clang-format)


https://github.com/FISCO-BCOS/FISCO-BCOS/blob/master/.clang-format


https://gitee.com/mirrors/FISCO-BCOS/blob/master/.clang-format


另外也有提供给VS使用的clang-format插件,VS2017主菜单-工具-选项-文本编辑器-C/C++-格式设置。


https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.ClangFormat


https://marketplace.visualstudio.com/items?itemName=MariuszBrzeski.CodeBeautifier


不过VS我个人推荐使用CodeMaid插件


https://marketplace.visualstudio.com/items?itemName=SteveCadwallader.CodeMaid



相关文章
|
9月前
|
存储 算法 C++
【Qt应用开发】复刻经典:基于Qt实现Windows风格计算器(加减乘除、删除、归零功能全解析)
在Qt中,"栈"的概念主要体现在两个层面:一是程序设计中的数据结构——栈(Stack),二是用户界面管理中的QStackedWidget控件。下面我将分别简要介绍这两个方面:
253 4
|
10月前
|
Windows
Windows下对于Qt中带 / 的路径的处理
Windows下对于Qt中带 / 的路径的处理
83 9
|
7月前
|
编译器 Windows
plugin cannot be loaded for module “QtQuick“ && Could not load the Qt platform plugin “windows“
本文讨论了在Qt应用程序中遇到的平台插件加载问题,包括具体的错误信息、解决方案和参考链接。问题表现为无法加载“QtQuick”模块的插件,并且无法找到“windows”平台插件。解决方案是修改环境变量`Qt5_DIR`以使用正确的Qt版本和编译器环境。
383 1
|
7月前
|
IDE 开发工具 C++
qt creator + vs2019编译记录
本文记录了作者在使用qt creator和vs2019编译项目时遇到的困难和解决方案,包括编译环境设置、qt creator编译脚本的成功案例、不带Ninja的编译脚本问题、错误示范以及相关参考链接。
235 0
qt creator + vs2019编译记录
|
8月前
|
安全 C++ Windows
Windows下C++使用gRPC(Qt和VS,含文件包和使用方法)
Windows下C++使用gRPC(Qt和VS,含文件包和使用方法)
|
10月前
Qt安装之后添加或移除组件(Qt Creator 10.0.1)
Qt安装之后添加或移除组件(Qt Creator 10.0.1)
554 2
|
10月前
Qt之HelloWord(Qt Creator 10)
Qt之HelloWord(Qt Creator 10)
|
10月前
|
IDE 开发工具 数据安全/隐私保护
【干货】Qt Creator快速下载、安装、使用教程
【干货】Qt Creator快速下载、安装、使用教程
|
10月前
|
C++
使用Qt Creator 出现的一些错误
使用Qt Creator 出现的一些错误
164 1
|
10月前
|
Windows
Qtdesigner报错:This application failed to stat could not find or load the Qt platform plugin “windows“
Qtdesigner报错:This application failed to stat could not find or load the Qt platform plugin “windows“