QT 配置格式化代码

Qt 配置Clang Format步骤

配置环境:Windows10 64bit + Qt5.12.8

1 启动Beautifier插件

菜单>帮助>关于插件

 

勾选后,重启Qt Creator

2 安装LVMM

主页:

根据自己的情况,选择下载相应的系统版本

https://releases.llvm.org/

 

 

 

 

 

 

下载完毕后,安装,在安装时,需勾选添加环境变量,便于后续操作。根据自己的情况选择安装路

径,然后一路下一步。

 

 

 

由于根据系统环境,选择安装了64位的LLVM,所以可能lldb.exe报找不到python36.dll,安装完

LLVM后,bin文件夹下没有python36.dll,所以需要下载对应版本的库,拷贝到D:\LLVM\bin(根据

自己安装的目录)

 

 

 

3 Qt中启用Clang Format

菜单>工具>选项

勾选第一项之后,在保存时会自动格式化当前的文件

 

 

 

 

 

4 编写自定义规则

创建了自定义的规则之后,规则文件在,其中dkwc++-baseon-google时我配置的名字:

C:\Users\Admin\AppData\Roaming\QtProject\qtcreator\beautifier\clangformat\dkwc++-

baseon-google

也可以直接使用编辑器进行编辑词文件

 

 

 

5 设置快捷键

可以设置为Ctrl+Alt+K,表示,在当前文件下按下这个快捷键,即可格式化当前的文件

 

 

 

6 参考的自定义规则

详细规则参考官方说明文档:

根据我们的编码规范,我在Google(工具已经内置的风格)的基础上,进行了调整。后续发现有遗漏

的可以一起来完善。

1 BasedOnStyle: Google

2

3 IndentWidth: 4

4 ColumnLimit: 80

5 AccessModifierOffset: -4

6 MaxEmptyLinesToKeep: 1

7 SpacesBeforeTrailingComments: 4

8

9 AllowAllArgumentsOnNextLine: true

10 AllowAllParametersOfDeclarationOnNextLine: false

11 AllowAllConstructorInitializersOnNextLine: false

12 AllowShortFunctionsOnASingleLine: false

13 AllowShortIfStatementsOnASingleLine: false

14 AllowShortBlocksOnASingleLine: false

15 AllowShortCaseLabelsOnASingleLine: false

16 AllowShortEnumsOnASingleLine: false

https://clang.llvm.org/docs/ClangFormatStyleOptions.html

17 AllowShortLambdasOnASingleLine: false

18 AllowShortLoopsOnASingleLine: false

19

20 AlignAfterOpenBracket: true

21 AlignTrailingComments: true

22 AlignEscapedNewlinesLeft: true

23 AlignOperands: true

24 AlignConsecutiveAssignments: true

25 AlignConsecutiveMacros: true

26 AlignEscapedNewlines: true

27

28 AlwaysBreakBeforeMultilineStrings: false

29

30 #BinPackArguments: true

31 #BinPackParameters: true

32

33 # space around ":"

34 BitFieldColonSpacing: Both

35

36 #BreakBeforeBraces: Allman

37 # if need custom brace wrapping

38 BreakBeforeBraces: Custom

39 BraceWrapping:

40 AfterControlStatement: Always

41 AfterCaseLabel: true

42 AfterClass: true

43 AfterFunction: true

44 AfterEnum: true

45 AfterStruct: true

46 AfterNamespace: true

47 AfterUnion: true

48 AfterExternBlock: true

49 BeforeElse: true

50 BeforeLambdaBody: true

51 BeforeCatch: true

52 BeforeWhile: true

53 SplitEmptyFunction: true

54 SplitEmptyRecord: true

55 SplitEmptyNamespace: true

56 # end of custom brace wrapping

57

58 BreakBeforeBinaryOperators: true

59 BreakBeforeTernaryOperators: true

60 BreakConstructorInitializers: AfterColon

61 #BreakInheritanceList: AfterColon

62 BreakStringLiterals: true

63

64 CompactNamespaces: false

65

66 ConstructorInitializerAllOnOneLineOrOnePerLine: false

67

68 #IndentPPDirectives: AfterHash

69

70 # true

71 # bool

72 # LooooooooooooooooooooooooooooooooooooooooooooooooooooongFunctionDeclaration();

73 # false

74 # bool

75 # LooooooooooooooooooooooooooooooooooooooooooooooooooooongFunctionDeclaration();

76 IndentWrappedFunctionNames: false

77

78 NamespaceIndentation: Inner

79

80 PointerAlignment: Left

81

82 SpaceAfterCStyleCast: false

83 SpaceAfterLogicalNot: false

84 SpaceAfterTemplateKeyword: false

85 SpaceBeforeSquareBrackets: false

86 SpaceInEmptyBlock: false

87 SpaceInEmptyParentheses: false

88 SpacesInParentheses: false

89 SpacesInSquareBrackets: false

90

91 SpaceBeforeAssignmentOperators: true

92 SpaceBeforeRangeBasedForLoopColon: true

93 SpacesInContainerLiterals: true

特殊情况

1 某些代码块不使用格式化

如果某个特殊的写法具有特定的含义,不需要进行格式化的化,可以使用如下方法

1 // clang-format off

2 不会被格式化的代码块

3 // clang-format on

参考连接

1. google编码规范

中文翻译:

2. LLVM

主页:

下载相应的系统版本

3. Artistic Style

主页:

4. 相关博客

Clang-format 配置文件

Qt Creator使用clang-format格式化代码

Qt高级——QtCreator代码格式化

https://zh-google-styleguide.readthedocs.io/en/latest/contents/

https://releases.llvm.org/

http://astyle.sourceforge.net/

https://blog.csdn.net/deeplee021/article/details/100877960

https://blog.csdn.net/m0_37194132/article/details/85134229

https://blog.51cto.com/9291927/2112402

Qt 配置Clang Format步骤

配置环境:Windows10 64bit + Qt5.12.8

1 启动Beautifier插件

菜单>帮助>关于插件

 

勾选后,重启Qt Creator

2 安装LVMM

主页:

根据自己的情况,选择下载相应的系统版本

https://releases.llvm.org/

 

 

下载完毕后,安装,在安装时,需勾选添加环境变量,便于后续操作。根据自己的情况选择安装路

径,然后一路下一步。

 

由于根据系统环境,选择安装了64位的LLVM,所以可能lldb.exe报找不到python36.dll,安装完

LLVM后,bin文件夹下没有python36.dll,所以需要下载对应版本的库,拷贝到D:\LLVM\bin(根据

自己安装的目录)

 

3 Qt中启用Clang Format

菜单>工具>选项

勾选第一项之后,在保存时会自动格式化当前的文件

 

 

4 编写自定义规则

创建了自定义的规则之后,规则文件在,其中dkwc++-baseon-google时我配置的名字:

C:\Users\Admin\AppData\Roaming\QtProject\qtcreator\beautifier\clangformat\dkwc++-

baseon-google

也可以直接使用编辑器进行编辑词文件

 

5 设置快捷键

可以设置为Ctrl+Alt+K,表示,在当前文件下按下这个快捷键,即可格式化当前的文件

 

7 参考的自定义规则

详细规则参考官方说明文档:

根据我们的编码规范,我在Google(工具已经内置的风格)的基础上,进行了调整。后续发现有遗漏

的可以一起来完善。

1 BasedOnStyle: Google

2

3 IndentWidth: 4

4 ColumnLimit: 80

5 AccessModifierOffset: -4

6 MaxEmptyLinesToKeep: 1

7 SpacesBeforeTrailingComments: 4

8

9 AllowAllArgumentsOnNextLine: true

10 AllowAllParametersOfDeclarationOnNextLine: false

11 AllowAllConstructorInitializersOnNextLine: false

12 AllowShortFunctionsOnASingleLine: false

13 AllowShortIfStatementsOnASingleLine: false

14 AllowShortBlocksOnASingleLine: false

15 AllowShortCaseLabelsOnASingleLine: false

16 AllowShortEnumsOnASingleLine: false

https://clang.llvm.org/docs/ClangFormatStyleOptions.html

17 AllowShortLambdasOnASingleLine: false

18 AllowShortLoopsOnASingleLine: false

19

20 AlignAfterOpenBracket: true

21 AlignTrailingComments: true

22 AlignEscapedNewlinesLeft: true

23 AlignOperands: true

24 AlignConsecutiveAssignments: true

25 AlignConsecutiveMacros: true

26 AlignEscapedNewlines: true

27

28 AlwaysBreakBeforeMultilineStrings: false

29

30 #BinPackArguments: true

31 #BinPackParameters: true

32

33 # space around ":"

34 BitFieldColonSpacing: Both

35

36 #BreakBeforeBraces: Allman

37 # if need custom brace wrapping

38 BreakBeforeBraces: Custom

39 BraceWrapping:

40 AfterControlStatement: Always

41 AfterCaseLabel: true

42 AfterClass: true

43 AfterFunction: true

44 AfterEnum: true

45 AfterStruct: true

46 AfterNamespace: true

47 AfterUnion: true

48 AfterExternBlock: true

49 BeforeElse: true

50 BeforeLambdaBody: true

51 BeforeCatch: true

52 BeforeWhile: true

53 SplitEmptyFunction: true

54 SplitEmptyRecord: true

55 SplitEmptyNamespace: true

56 # end of custom brace wrapping

57

58 BreakBeforeBinaryOperators: true

59 BreakBeforeTernaryOperators: true

60 BreakConstructorInitializers: AfterColon

61 #BreakInheritanceList: AfterColon

62 BreakStringLiterals: true

63

64 CompactNamespaces: false

65

66 ConstructorInitializerAllOnOneLineOrOnePerLine: false

67

68 #IndentPPDirectives: AfterHash

69

70 # true

71 # bool

72 # LooooooooooooooooooooooooooooooooooooooooooooooooooooongFunctionDeclaration();

73 # false

74 # bool

75 # LooooooooooooooooooooooooooooooooooooooooooooooooooooongFunctionDeclaration();

76 IndentWrappedFunctionNames: false

77

78 NamespaceIndentation: Inner

79

80 PointerAlignment: Left

81

82 SpaceAfterCStyleCast: false

83 SpaceAfterLogicalNot: false

84 SpaceAfterTemplateKeyword: false

85 SpaceBeforeSquareBrackets: false

86 SpaceInEmptyBlock: false

87 SpaceInEmptyParentheses: false

88 SpacesInParentheses: false

89 SpacesInSquareBrackets: false

90

91 SpaceBeforeAssignmentOperators: true

92 SpaceBeforeRangeBasedForLoopColon: true

93 SpacesInContainerLiterals: true

特殊情况

1 某些代码块不使用格式化

如果某个特殊的写法具有特定的含义,不需要进行格式化的化,可以使用如下方法

1 // clang-format off

2 不会被格式化的代码块

3 // clang-format on

参考连接

1. google编码规范

中文翻译:

2. LLVM

主页:

下载相应的系统版本

3. Artistic Style

主页:

4. 相关博客

Clang-format 配置文件

Qt Creator使用clang-format格式化代码

Qt高级——QtCreator代码格式化

https://zh-google-styleguide.readthedocs.io/en/latest/contents/

https://releases.llvm.org/

http://astyle.sourceforge.net/

https://blog.csdn.net/deeplee021/article/details/100877960

https://blog.csdn.net/m0_37194132/article/details/85134229

https://blog.51cto.com/9291927/2112402

posted @ 2022-03-18 17:16  一笔一世界  阅读(1970)  评论(0编辑  收藏  举报