Especially in larger SW projects, consistency is a very important factor. And one main area of consistency is the code style.

Usually in projects with several people involved, a code style is written down. Like for example the one the Qt project has: https://wiki.qt.io/Qt_Coding_Style. This code style is usually checked manually in code reviews.

I have often seen ongoing debates about the style in reviews. Especially for new projects, or new members. Automating that task of the styling would make the life of new project members and reviewers a lot easier. Luckily such tools exist even for C++.

The currently best one is probably clang-format. One great feature is, that its configuration (code style) can be easily shared in the code repository. All that is needed is a .clang-format (or _clang-format) file containing the configuration. One example of a .clang-format configuration that matches the Qt style very well (as Qt might use it mandatory in the future) can be found here: http://code.qt.io/cgit/qt/qt5.git/tree/_clang-format. Many IDEs and editors have support for that tool. QtCreator as well. So it’s easy for new/all members of a project to comply the coding style. And here is a short tutorial on how to configure QtCreator to use clang-format:

First you need to have clang-format installed. For Windows, it is included in the clang package http://releases.llvm.org/download.html. Linux distributions usually have packages included already. MacOS users can for example use brew to install it.

Next is to create a .clang-format file in the root directory of your project. Configuration options can be found here: https://clang.llvm.org/docs/ClangFormatStyleOptions.html. There is even a (very slow) tool to generate a configuration out of your existing code https://github.com/johnmcfarlane/unformat.

In QtCreator the beautifier plugin needs to be enabled in “Help -> About Plugins…” 

In the QtCreator settings, the beautifier plugin needs to use clang-format. And optionally enable to format on save automatically (highly recommended).

Next configure clang-format to use the configuration file. And set the path to the clang-format executable if needed. 

Optionally you can format the code manually using a keyboard shortcut. Therefore a shortcut needs to be set.

Now you are ready to enjoy great automatic code styling in QtCreator!