When you type text or code, it is indented automatically according to the selected text editor or code style options. Select a block to indent it when you press Tab . Press Shift+Tab to decrease the indentation. You can disable automatic indentation.
You can specify indentation for:
You can also specify indentation separately for each project. You can specify several sets of code style settings and easily switch between them. In addition, you can import and export code style settings.
To automatically fix indentation according to the indentation settings when you save the file, select 工具 > Options > 文本编辑器 > Behavior > Clean whitespace > Clean indentation . Select the Skip clean whitespace for file types check box to exclude the specified file types.
To specify indentation settings for the C++ editor:
You can specify how to:
const
or
volatile
关键词。
You can use the live preview to see how the options change the indentation.
To specify different settings for a particular project, select Projects > Code Style Settings .
The Clang Format plugin uses the LibFormat library for automatic formatting and indentation.
To enable the plugin, select Help > About Plugins > C++ > ClangFormat . Then select Restart Now to restart Qt Creator and load the plugin.
注意: If you enable the plugin, do not use the Beautifier , because combining the two can provide unexpected results.
You can use Clang Format to enforce a coding style for a project or the whole organization. Create a
.clang-format
file that contains the
Clang Format Style Options
to use and save it in the root folder of the project or one of its parent folders. The plugin searches for the Clang format file recursively from the directory that contains the source file up to the file system root.
To specify settings for automatic formatting and indentation:
注意: This is not recommended, because it defeats the purpose of a Clang format file.
To override the
.clang-format
file for a project, select
Projects
>
Project Settings
>
Code Style
>
Override Clang Format configuration file
.
You can create
.clang-format
files that contain 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
To specify settings for the Qt Quick editor:
You can specify how to interpret the Tab key presses and how to align continuation lines.
To specify different settings for a particular project, select Projects > Code Style Settings .
To specify settings for the Nim editor (experimental):
You can specify how to interpret the Tab key presses and how to align continuation lines.
To specify different settings for a particular project, select Projects > Code Style .
To specify indentation settings for text files that do not contain C++ or QML code (such as Python code files), select 工具 > Options > 文本编辑器 > Behavior .
To specify different settings for a particular project, select Projects > Editor .
You can specify how to interpret the Tab and Backspace key presses and how to align continuation lines.
You can specify tab settings at the following levels:
You can specify tab policy and tab size in the Tabs and Indentation group. In the Tab policy field, select whether to use only spaces or only tabs for indentation, or to use a mixture of them.
By default, the tab length in code editor is 8 spaces and the indent size is 4 spaces. You can specify the tab length and indent size separately for each project and for different types of files.
You can have continuation lines aligned with the previous line. In the Align continuation lines field, select Not at all to disable automatic alignment and indent continuation lines to the logical depth. To always use spaces for alignment, select With Spaces . To follow the Tab policy , select With Regular Indent .
When you type text or code, it is indented automatically according to the selected text editor or code style options. To specify typing options, select 工具 > Options > 文本编辑器 > Behavior > Typing .
To disable automatic indentation, deselect the Enable automatic indentation check box.
You can specify how the indentation is decreased when you press Backspace 在 Backspace indentation field. To go back one space at a time, select None . To decrease indentation in leading white space by one level, select Follows Previous Indents . To move back one tab length if the character to the left of the cursor is a space, select Unindents .
You can specify whether the Tab key automatically indents text when you press it. To automatically indent text, select Always 在 Tab key performs auto-indent field. To only indent text when the cursor is located within leading white space, select In Leading White Space .
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
=
or
+=
, 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
if
语句。
To bind pointers (
*
) and references (
&
) in types and declarations to identifiers, type names, or left or right
const
or
volatile
keywords, select the check boxes in the
Pointers and References
tab.
The
*
and
&
characters are automatically bound to identifiers of pointers to functions and pointers to arrays.
To prefer getter names without the string get , select the Prefer getter names without "get" check box in the Getter and Setter tab.