网站出售/泸州网站优化推广
在开发过程中为了使每个人的开发代码结构保持一致,我们通常会有一些约定,代码的格式化。
QtCreator开发工具有一个很好的插件工具可以使得每个开发者的代码风格保持一致。
我们来看看如何配置吧!
安装clang-format
安装clang-format简单,使用一条shell命令就可以完成
sudo apt install clang-format
安装Beautifier插件
配置Beautifier插件
完成
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: true
BinPackParameters: true
BraceWrapping: AfterClass: falseAfterControlStatement: falseAfterEnum: falseAfterFunction: trueAfterNamespace: falseAfterObjCDeclaration: falseAfterStruct: falseAfterUnion: falseAfterExternBlock: falseBeforeCatch: falseBeforeElse: falseIndentBraces: falseSplitEmptyFunction: trueSplitEmptyRecord: trueSplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros: - foreach- Q_FOREACH- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories: - Regex: '^<ext/.*\.h>'Priority: 2- Regex: '^<.*\.h>'Priority: 1- Regex: '^<.*'Priority: 2- Regex: '.*'Priority: 3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Never
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
RawStringFormats: - Language: CppDelimiters: - cc- CC- cpp- Cpp- CPP- 'c++'- 'C++'CanonicalDelimiter: ''BasedOnStyle: google- Language: TextProtoDelimiters: - pb- PB- proto- PROTOEnclosingFunctions: - EqualsProto- EquivToProto- PARSE_PARTIAL_TEXT_PROTO- PARSE_TEST_PROTO- PARSE_TEXT_PROTO- ParseTextOrDie- ParseTextProtoOrDieCanonicalDelimiter: ''BasedOnStyle: google
ReflowComments: true
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
TabWidth: 4
UseTab: Never
别忘记点击应用。
Beautifier使用
方法一:配置完成后需要重启QtCreator,然后在保存文件的时候会自动将当前文件进行格式化。
方法二:可以在工具中找到clangFormat格式化当前文件。