aboutsummaryrefslogtreecommitdiffstats
path: root/doc/qtcreator/src/editors/creator-only/creator-preferences-cpp-code-style.qdoc
blob: 56e413b7e935460469c41df79965e2f5a877afa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only

/*!
    \page creator-preferences-cpp-code-style.html
    \previouspage creator-reference.html

    \ingroup creator-reference-preferences-cpp

    \title C++ Code Style

    \brief Set global code style for C++ files.

    \QC uses the Clang \l{https://clang.llvm.org/docs/LibFormat.html}{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:

    \list 1
        \li Go to \preferences > \uicontrol {C++}.
            \image qtcreator-code-style-clang-format-global.webp {Code Style preferences}
        \li In \uicontrol {Formatting mode}, select:
            \list
                \li \uicontrol {Indenting Only} to only indent code.
                \li \uicontrol {Full Formatting} to use the \key {Ctrl+I}
                    keyboard shortcut to format code instead of indenting.
                \li \uicontrol {Use Built-In Indenter} to turn off ClangFormat.
            \endlist
        \li Select \uicontrol {Ignore files greater than} to make parsing faster
            by ignoring big files. Specify the maximum size of files to parse.
        \li To apply the formatting while you type, select
            \uicontrol {Format while typing}.
        \li To apply the formatting to the edited code when you save the file,
            select \uicontrol {Format edited code on file save}.
        \li To change the ClangFormat style globally for all projects,
            select \uicontrol {Use custom settings}.
        \li In \uicontrol {Custom settings}, select the settings to change, and
            then select \uicontrol Copy.
        \li Give a name to the settings, and select \uicontrol OK.
        \li In \uicontrol ClangFormat, edit the
            \l{https://clang.llvm.org/docs/ClangFormatStyleOptions.html}
            {ClangFormat Style Options}. The live preview shows how the
            preferences change the indentation.
            If you enter invalid values, you see warning messages.
    \endlist

    \section1 Using Built-In Indenter

    \image qtcreator-code-style-built-in-indenter.webp {Code Style preferences for built-in indenter}

    If you select \uicontrol {Use Built-In Indenter} in
    \uicontrol {Formatting mode}, you can specify how to:

    \list
        \li Interpret the \key Tab and \key Backspace key presses.
        \li Indent the contents of classes, functions, blocks, and namespaces.
        \li Indent braces in classes, namespaces, enums, functions, and blocks.
        \li Control switch statements and their contents.
        \li Align continuation lines.
        \li Bind pointers (*) and references (&) in types and declarations to
            identifiers, type names, or left or right \c const or \c volatile
            keywords.
    \endlist

    \section2 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 functions and blocks and declarations
    within namespaces.

    \image qtcreator-code-style-content.png {Content preferences}

    \section2 Specifying Settings for Braces

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

    \image qtcreator-code-style-braces.png {Braces preferences}

    \section2 Specifying Settings for Switch Statements

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

    \image qtcreator-code-style-switch.png {Switch preferences}

    \section2 Specifying Alignment

    To align continuation lines to tokens after assignments, such as \c = or
    \c +=, select \uicontrol {Align after assignments}. You can
    specify additional settings for aligning continuation lines in the
    \uicontrol General tab.

    You can also add spaces to conditional statements, so that they are not
    aligned with the following line. Usually, this only affects \c if
    statements.

    \image qtcreator-code-style-alignment.png {Alignment preferences}

    \section2 Binding Pointers and References

    To bind pointers (\c *) and references (\c &) in types and declarations to
    identifiers, type names, or left or right \c const or \c volatile keywords,
    select the check boxes in the \uicontrol {Pointers and References} tab.

    The \c * and \c & characters are automatically bound to identifiers of
    pointers to functions and pointers to arrays.

    \image qtcreator-pointers-references.png {Pointers and References preferences}

    \section1 Creating ClangFormat Files from Command Line

    You can create \c {.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:

    \badcode
    clang-format -style=llvm -dump-config > .clang-format
    \endcode

    \sa {Indent text or code}, {Specify code style}, {Behavior},
    {Qt Quick Code Style}, {Nim}
*/