Skip to content

Instantly share code, notes, and snippets.

@Uflex
Last active November 3, 2019 16:19
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save Uflex/7086827 to your computer and use it in GitHub Desktop.
Save Uflex/7086827 to your computer and use it in GitHub Desktop.
Configuration of clang-format for QtCreator

Clang-format in QtCreator

Edit: QtCreator 3.1 introduced a new plugin called Beautifier that supports clang-format. You still have to install the clang tools separately but you don't have to add them as external tools anymore. The plugin can be configured in the options dialog under the beautifier tab > Clang Format. Add a new style and copy the configuration below without the curly braces.

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.

Installation

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.

EDIT: It seems that there are now official builds for most OSes on the download page: http://llvm.org/releases/download.html

Configuration

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)
  • Input: %{CurrentDocument:Selection}

If you want to correct the whole file, leave the input field empty, tick the "Modifies current document", 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):

You can assign a shortcut to that command (I personally use Ctrl+Shift+I on Windows and ⌘⌥I on Mac 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! :)

@NLKNguyen
Copy link

Thank you! It works nicely.

@cyc1ingsir
Copy link

Thanks! This definitely put me on the right track!
However, for the beautifier entry to appear within the Tools->Options the Beautifier plugin must be enabled first. With version 3.5.x it's still not enabled by default.
Enabeling can be done via Help -> About Plugins.
Ctrl+Shift+I collides with Open Include History (on QtCreator-Linux at least). I've tied this to Ctrl+Alt+I.
Also, if you want to modify the default styles, check [X] Use customized styles.
In the dialog appearing when clicking add just set a name of your choice and within the value field, add a comma separated list of options. e.g.
BasedOnStyle: llvm, ColumnLimit: 100, IndentWidth: 4, Standard: Cpp11, PointerBindsToType: false, BreakBeforeBraces: Linux, BreakConstructorInitializersBeforeComma: true, AccessModifierOffset: -4, BreakBeforeBinaryOperators: true ...
I've found (http://clangformat.com/) quite useful for explaining the options.

@kogorek
Copy link

kogorek commented Mar 11, 2017

Thank you!

@ccseer
Copy link

ccseer commented Jun 12, 2017

Thank you! *2

@pikapi12321
Copy link

nice~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment