原由在於習慣性的想格式化代碼,發現Qt Creater默認竟然是沒有代碼格式化的,只有一個縮進,搞毛線啊!!!html
搜索了下,卻是很容易就搜到了,Qt Creater中有個插件:beautifier,在 幫助-關於插件 中開啓了便可(須要重啓)。工具
惋惜這只是一個接口,它的做用是調用格式化工具進行格式化。--這一點折騰了兩個小時才意識到!慘痛的教訓告訴咱們,必定要看官方文檔,別老想着走捷徑!oop
按照官方的說法,它支持三種外部工具:Artistic Style、ClangFormat、Uncrustify 。而且都提供了下載地址。spa
這裏僅以 ClangFormat 來講明。插件
ClangFormat 是LLVM的一個子功能,LLVM是相似GCC的東西。3d
因此,想用 ClangFormat ,就須要下載LLVM,根據上面的地址,搜索適合本身的版本便可。我這裏是win64位的。code
安裝的時候PATH選項無所謂,由於Qt Creater的beautifier 的Clang Format 選項中既能夠根據PATH搜索,也能夠自行指定路徑。orm
安裝好LLVM以後,就能夠設置 Clang Format 選項了。htm
先配置路徑(如:C:\Program Files\LLVM\bin\clang-format.exe );blog
再選擇代碼樣式,默認有LLVM、Google、Chromium、Mozilla、WebKit、File,此外還能夠自定義。
默認樣式,須要重點說一下File -- 由於其餘的樣式都是給定的樣式,而 File 則不是。
File 是 clang-format.exe -style=file 的意思,意味着 clang-format.exe 會去搜索樣式文件( *.clang-format 文件)。
須要注意的是,它的搜索路徑是當前文件所在的文件夾或者當前項目(不知道是否會繼續往上層搜索--至少,工做空間就沒驗證成功)。
這樣的話,它要求把 .clang-format 文件放在項目文件夾中,與個人習慣不符。
自定義樣式
所幸,還有一個選擇:Use customized style - add 。
以下
Value 部份內容以下
AccessModifierOffset: 0 AlignEscapedNewlinesLeft: false AlignTrailingComments: true AllowAllParametersOfDeclarationOnNextLine: false AllowShortFunctionsOnASingleLine: false AllowShortIfStatementsOnASingleLine: false AllowShortLoopsOnASingleLine: false AlwaysBreakBeforeMultilineStrings: false AlwaysBreakTemplateDeclarations: false BinPackParameters: false BreakBeforeBinaryOperators: false BreakBeforeTernaryOperators: false BreakConstructorInitializersBeforeComma: false ColumnLimit: 128 BreakBeforeBraces: Attach CommentPragmas: '' ConstructorInitializerAllOnOneLineOrOnePerLine: false ConstructorInitializerIndentWidth: 0 ContinuationIndentWidth: 0 Cpp11BracedListStyle: false DerivePointerBinding: false IndentCaseLabels: true IndentFunctionDeclarationAfterType: false IndentWidth: 4 Language: Cpp MaxEmptyLinesToKeep: 1 NamespaceIndentation: None ObjCSpaceAfterProperty: true ObjCSpaceBeforeProtocolList: true ObjCBlockIndentWidth: 4 PenaltyBreakBeforeFirstCallParameter: 100 PenaltyBreakComment: 100 PenaltyBreakFirstLessLess: 0 PenaltyBreakString: 100 PenaltyExcessCharacter: 1 PenaltyReturnTypeOnItsOwnLine: 20 SpaceBeforeAssignmentOperators: true SpaceBeforeParens: ControlStatements SpaceInEmptyParentheses: false SpacesBeforeTrailingComments: 1 SpacesInAngles: false SpacesInCStyleCastParentheses: false SpacesInContainerLiterals: false SpacesInParentheses: false TabWidth: 4 UseTab: Never
ps:上面這段配置是搜來的,忘了出處了~
使用
工具-Beautifier-ClangFormat 裏面有兩個選項,分別是格式化當前文件、格式化選定內容。
可是,這樣使用很繁瑣,很難用。因此最好給它設置一個快捷鍵,個人是ALT+CTRL+L(intellij的快捷鍵)。
注意,ClangFormat 有個特殊的選項 Format entire file if no text was selected ,建議選中這個選項,這樣,只須要設置一個Format Selected Text 快捷鍵便可。
參考: