C++ Code Style
Qt Creator uses the Clang LibFormat library to automatically format and indent C++ code. It enforces a coding style for a project or the whole organization.
To specify global indentation settings for the C++ editor:
- Select Preferences > C++.
- In Formatting mode, select:
- Indenting Only to only indent code.
- Full Formatting to use the Ctrl+I keyboard shortcut to format code instead of indenting.
- Disable to turn off ClangFormat.
- To apply the formatting while you type, select Format while typing.
- To apply the formatting to the edited code when you save the file, select Format edited code on file save.
- To change the ClangFormat style globally for all projects, select Override ClangFormat configuration file.
- In the Current settings field, select the settings to modify and click Copy.
- Give a name to the settings and click OK.
- Click Edit to set ClangFormat Style Options.
In the other tabs, you can specify how to:
- Interpret the Tab and Backspace key presses.
- Indent the contents of classes, functions, blocks, and namespaces.
- Indent braces in classes, namespaces, enums, functions, and blocks.
- Control switch statements and their contents.
- Align continuation lines.
- Bind pointers (*) and references (&) in types and declarations to identifiers, type names, or left or right
- Name getter functions.
Use the live preview to see how the preferences change the indentation.
You can indent public, protected, and private statements and declarations related to them within classes.
You can also indent statements within functions and blocks and declarations within namespaces.
You can indent class, namespace, enum and function declarations and code blocks.
You can indent case or default statements, or statements or blocks related to them within switch statements.
To align continuation lines to tokens after assignments, such as
+=, select the Align after assignments check box. You can specify additional settings for aligning continuation lines in the General tab.
You can also add spaces to conditional statements, so that they are not aligned with the following line. Usually, this only affects
To bind pointers (
*) and references (
&) in types and declarations to identifiers, type names, or left or right
volatile keywords, select the check boxes in the Pointers and References tab.
& characters are automatically bound to identifiers of pointers to functions and pointers to arrays.
To override the
.clang-format file for a particular project, create a copy of the built-in style and edit its settings by selecting Projects > Project Settings > Code Style > Copy > Edit > ClangFormat > Override ClangFormat configuration file.
You can create
.clang-format files that have the configuration options of a certain predefined style from the command line. For example, to create a format file for the LLVM style, enter the following command:
clang-format -style=llvm -dump-config > .clang-format
© 2023 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.