Starting with Qt Creator 4.8 we ship the new ClangFormat plugin based on the LibFormat library. In 4.9 it is not experimental anymore but still disabled by default. Let's shortly recall the history of ClangFormat support and why we've decided to have a new plugin.

Beautifier plugin

The clang-format tool could already be used in Qt Creator before as part of the experimental Beautifier plugin along with Uncrustify and Artistic Style. If you have this plugin enabled you can simply invoke the executable by the shortcut or apply formatting on save. Pretty straight forward. You edit your code, you format it when you're done. We're not planning to remove any of this functionality because it serves its purpose pretty well and can be customized to the clang-format version that you prefer to use in your project.

But Beautifier does not cover other important cases, it's only a tool to finalize your code.

Indentation with ClangFormat

I assume that you already use Ctrl+I to have your code indented. The same thing happens when you press Return or move/paste some code into the C++ editor. Since ages Qt Creator has its own indentation engine to provide you the proper white space where necessary. At least it should be proper. But the reality is that Qt Creator indentation still does not fully support modern C++. For example we already have these bugs for quite a long time Qt Creator indentation bugs and they always raise our desire to fix them altogether.

So the main purpose of our new ClangFormat plugin is to indent the code properly. But it can potentially bring much more in the future.

To enable the plugin go to Help > About Plugins, click on ClangFormat and restart your Qt Creator.

Features

With the plugin you can not only indent but also apply standard clang-format formatting which will optimize lines if possible, remove trailing white space and empty lines, etc. We have decided that it's a good idea to apply formatting on the same hotkey as indentation (Ctrl+I) but only when the checkbox is selected in Tools > Options > C++ > Code Style > 'Format instead of indenting'. There are a few more checkboxes to customize the plugin behavior. 'Format while typing' will try to reformat code automatically if possible and 'Format edited code on file save' will go through and format the edited lines in the file you are saving.

By default the plugin uses the .clang-format configuration which it finds according to the standard clang-format rules. You can override them with either global Qt Creator settings or with specific settings for your project.

Limits

Because clang-format was never meant to be used as an indentation tool we have to use a modified version of the LibFormat library. It helps us to keep the line breaks if we are sure they must remain. To force indentation for empty lines we have to supply clang-format with some extra code which should behave as if it's a valid code. In addition clang-format can be confused by incomplete code so if you have something like this:



int main() {

foo(//Press CR here

}



the default clang-format won't properly format the parameters until you provide it a closing ')'. In such cases we try to predict the possible code and trigger the formatting with modified text but it is not very reliable yet. So if you encounter problems like that you might just manually press Ctrl+I to reformat such piece of code (or save the file) when the statement is complete.

Get Qt Creator 4.9.0

The opensource version is available on the Qt download page, and you find commercially licensed packages on the Qt Account Portal. Qt Creator 4.9.0 is also available through an update in the online installer. The Qt Creator Manual 4.9 is available here. Please post issues in our bug tracker. You can also find us on IRC on #qt-creator on chat.freenode.net, and on the Qt Creator mailing list.