qt creator 配置clang-format 代码风格

1 Qt creator配置格式化代码插件clang-format

 

 

2 clang-format的配置


qt creator上面的选项卡:帮助>关于插件,在c++的那一栏中勾选Beautifier,然后重启qt creator的软件。

 

 

上方的选项卡:编辑>Preferences>美化器

选择工具为ClangFormat

 

 

在Clang Format的那一栏中,选择clang-format.exe的路径

1, 自己下个LLVM的编译器

LLVM Download Page

 

 

选择自己需要的版本

 

 

 

2, 我是在Qt Creator的安装路径下面找了个clang-format.exe的路径,填了上去,看有的文章中是自己下个LLVM的编译器,我没有试过。

 

 

可以使用预定义的代码风格,但这些预定的样式,和我们个人的使用习惯有些不一样,可以使用自定义风格,

 

点击"添加",给自定义的样式取个名称,我写的是"MyStyle",然后把样式的具体值放在下面。

 

 

 

 

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

我自己的配置:

BasedOnStyle: Microsoft
AccessModifierOffset: -4
AlignConsecutiveMacros: true
AlignTrailingComments: true
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: false
BreakBeforeBraces: Attach #代码风格

ColumnLimit: 0

 

再该文件的基础上,修改自己想要的参数:

# 编程语言: 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

 

 

 

关于这个代码风格的格式化,

 

Attach的风格

 

 

 

Allman的风格

 

 

posted @ 2025-03-12 09:24  石门  阅读(409)  评论(0)    收藏  举报