QtCreator doesn't allow using clang-format by default. Watch QTCREATORBUG-10111 for a better integration. As of today, you should use it as an external tool, either replacing your selection or the entire file. If you choose to replace the file, you will have to save it before launching the tool and QtCreator will reload the file, making it impossible to undo/redo afterwards. If you choose to replace your selection, you often have to select the whole function, otherwise you will lose the indentation; clang-format doesn't seem to keep the leading spaces. When you run clang-format on your selection, Creator marks the whole selection as changed, even if nothing moved. This is a minor inconvenience and anyway the "Auto-indent Selection" built-in with Creator does the same thing.
For Windows, download LLVM from http://llvm.org/builds/. You don't need to add it to the PATH, you can use it with the absolute path.
In QtCreator, add an external tool (Tools > Options... > Environment > External Tools > Add > Add Tool).
- Arguments:
-style="{BasedOnStyle: llvm, AccessModifierOffset: -4, BreakBeforeBinaryOperators: true, ColumnLimit: 80, Standard: Cpp11, IndentWidth: 4, BreakBeforeBraces: Linux}"
- Working dir:
%{CurrentProject:Path}
(seems to be optional, works without it) - Output: replace selection
- Error output: show in pane
- Don't tick the "Modifies current document" (this will ask you to save the document before running the tool and should be ticked if you want to update the whole file instead of only the selection)
- Input:
%{CurrentDocument:Selection}
If you want to correct the whole file, leave the input field empty, set the output to "Show in Pane" and add -i %{CurrentDocument:FilePath}
to the arguments.
See these links for all possible parameters and enum values (that are not listed in the -help option):
- http://clang.llvm.org/doxygen/structclang_1_1format_1_1FormatStyle.html
- http://clang.llvm.org/docs/ClangFormat.html
You can assign a shortcut to that command (I personally use Ctrl+Shift+I
to keep the built-in formatting tool accessible). Go to Tools > Options... > Environment > Keyboard
and at the very bottom there is the Tools category.
Enjoy! :)
Thank you! It works nicely.