Qt Creator Manual

Indenting Code

When you type code, it is indented automatically according to the selected text editor and 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 either globally for all files or separately for:

  • Text files
  • C++ files
  • QML files

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.

Indenting Text Files

To specify global indentation settings for the text editor, select Tools > Options > Text Editor > Behavior. You can also use these settings globally for all editors and files.

"Text Editor Behavior options"

To specify settings for a particular project, select Projects > Editor Settings.

You can specify how to interpret the Tab and Backspace key presses and how to align continuation lines.

Indenting C++ Files

To specify indentation settings for the C++ editor:

  1. Select Tools > Options > C++.
  2. In the Current settings field, select the settings to modify and click Copy.

    "C++ Code Style options"

  3. Give a name to the settings and click OK.
  4. Click Edit to specify code style settings for the project.

    "Edit Code Style Settings dialog"

You can specify how to:

  • Interpret the Tab and Backspace key presses.
  • Indent the contents of classes, methods, blocks, and namespaces.
  • Indent braces in classes, namespaces, enums, methods, and blocks.
  • Control switch statements and their contents.
  • Align continuation lines.

You can use the live preview to see how the options change the indentation.

To specify the settings for a particular project, select Projects > Code Style Settings.

Indenting QML Files

To specify global settings for the Qt Quick editor:

  1. Select Tools > Options >Qt Quick.
  2. In the Current settings field, select the settings to modify and click Copy.

    "QML Code Style options"

  3. Give a name to the settings and click OK.
  4. Click Edit to specify code style settings for the project.

    "Edit Code Style Settings dialog"

You can specify how to interpret the Tab key presses and how to align continuation lines.

To specify the settings for a particular project, select Projects > Code Style Settings.

Specifying Tab Settings

You can specify tab settings at the following levels:

  • Global settings for all files
  • Global C++ settings for C++ files
  • Global Qt Quick settings for QML files
  • Project specific settings for all editors of files in the project
  • Project specific settings for C++ files in the project
  • Project specific settings for QML files in the project

Specifying Tabs and Indentation

You can specify tab policy and tab size in the Typing 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.

Speficying Typing Options

When you type code, it is indented automatically according to the selected text editor and code style options. Specify typing options in the Typing group. To disable automatic indentation, deselect the Enable automatic indentation check box.

You can specify how the indentation is decreased when you press Backspace in the 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 in the Tab key performs auto-indent field. To only indent text when the cursor is located within leading white space, select In Leading White Space.

Specifying Settings for Content

You can indent public, protected, and private statements and declarations related to them within classes.

You can also indent statements within methods and blocks and declarations within namespaces.

"Content options"

Specifying Settings for Braces

You can indent class, namespace, enum and method declarations and code blocks.

"Braces options"

Specifying Settings for Switch Statements

You can indent case or default statements, or statements or blocks related to them within switch statements.

"Switch options"

Specifying Alignment

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 statements.

"Alignment options"