Clang-format is a plug-in for formatting code that can be used to format C/C ++ / Java/JavaScript/Objective-C/Protobuf/C # code. In Qt Creator, the formatting code only supports very simple indentation correction. This blog mainly describes how to configure the clang-format plug-in on Qt Creator.

Open the Beautifier plug-in

Open theHelp -> About plug-ins -> Tick BeautifierAnd then restart Qt Creator. Note that you must restart Qt Creator for the Beautifier plug-in to take effect.

Load the clang-format plug-in

First of all toLLVM download siteTo download the clang-format executable file. In this case, you need to choose the appropriate version according to your operating system and computer configuration. I downloaded the Windows 64-bit version.After downloading the installation package, double-click install. Can customize the installation path, I was installed to D disk, here had better remember your installation path.

Open Qt Creator and clickTools -> Options ->Beautifier->Clang-format. Click Browse, a Windows file window will pop up, findclang-format.exeDouble-click the file and Qt Creator will fill it in automaticallyclang-format.exeThe absolute path to the file. Of course, you can also choose to enter it manuallyclang-format.exeThe absolute path to the file. And then clickUse customized style->Add.In the new window that pops up, write the code formatting style you want. Refer to the SettingsClang-format Official document. My format is as follows:

{
BasedOnStyle: Google,
AccessModifierOffset: 2 -,
AlignAfterOpenBracket: Align,
AlignConsecutiveAssignments: false,
AlignConsecutiveDeclarations: false,
AlignEscapedNewlines: DontAlign,
AlignOperands: true,
AllowAllParametersOfDeclarationOnNextLine: true,
AllowShortBlocksOnASingleLine: true,
AllowShortCaseLabelsOnASingleLine: true,
AllowShortFunctionsOnASingleLine: All,
AllowShortIfStatementsOnASingleLine: false,
AllowShortLoopsOnASingleLine: false,
AlwaysBreakAfterDefinitionReturnType: None,
AlwaysBreakAfterReturnType: None,
AlwaysBreakBeforeMultilineStrings: false,
AlwaysBreakTemplateDeclarations: true,
AlwaysBreakAfterDefinitionReturnType: None,
AlwaysBreakAfterReturnType: None,
AlwaysBreakBeforeMultilineStrings: false,
AlwaysBreakTemplateDeclarations: true,
BinPackArguments: true,
BinPackParameters: true,
BreakBeforeBinaryOperators: None,
BreakBeforeBraces: Attach,
BreakBeforeTernaryOperators: false,
BreakConstructorInitializers: AfterColon,
BreakStringLiterals: true,
ColumnLimit: 120,
CommentPragmas: '^ IWYU pragma:',
CompactNamespaces: false,
ConstructorInitializerAllOnOneLineOrOnePerLine: false,
ConstructorInitializerIndentWidth: 4,
ContinuationIndentWidth: 4,
Cpp11BracedListStyle: true,
DerivePointerAlignment: false,
DisableFormat: false,
ExperimentalAutoDetectBinPacking: false,
FixNamespaceComments: true,
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ],
IncludeBlocks: Regroup,
IndentCaseLabels: true,
IndentPPDirectives:  AfterHash,
IndentWidth: 4,
IndentWrappedFunctionNames: true,
KeepEmptyLinesAtTheStartOfBlocks: false,
MacroBlockBegin: '',
MacroBlockEnd: '',
MaxEmptyLinesToKeep: 1,
NamespaceIndentation: All,
ObjCBlockIndentWidth: 4,
ObjCSpaceAfterProperty: false,
ObjCSpaceBeforeProtocolList: true,
PenaltyBreakBeforeFirstCallParameter: 19,
PenaltyBreakComment: 300,
PenaltyBreakFirstLessLess: 120,
PenaltyBreakString: 1000,
PenaltyExcessCharacter: 1000000,
PenaltyReturnTypeOnItsOwnLine: 60,
PointerAlignment: Left,
ReflowComments: true,
SortIncludes: true,
SpaceAfterCStyleCast: false,
SpaceAfterTemplateKeyword: true,
SpaceBeforeAssignmentOperators: true,
SpaceBeforeParens: ControlStatements,
SpaceInEmptyParentheses: false,
SpacesBeforeTrailingComments: 2,
SpacesInAngles: false,
SpacesInContainerLiterals: false,
SpacesInCStyleCastParentheses: false,
SpacesInParentheses: false,
SpacesInSquareBrackets: false,
TabWidth: 4,
UseTab: Never,
}
Copy the code

The Clang-format plug-in is installed. Hurry up and start a new one.hello worldGive it a try. Original code:Click on theTools ->Beautifier->ClangFormat->Format Current File, you can format the code.After formatting, the code format is as follows:

Set shortcut keys

Each formatting requires a lot of mouse clicks, some trouble. We can set a shortcut for the formatting code function. Click on theTools -> Options -> Environment -> Keyboard.Search Clang and findFormatFileAt the bottomKey sequenceFill in the shortcut key you want. I set it toAlt+Shift+FJust click OK.