aboutsummaryrefslogtreecommitdiffstats
path: root/doc/qtcreator/src/editors/creator-only/creator-preferences-cpp-code-style.qdoc
blob: 44622426af86a63fda37879aa1793bf1dfbbd963 (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
127
128
// Copyright (C) 2023 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 Select \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 Disable to turn off ClangFormat.
            \endlist
        \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 {Override ClangFormat configuration file}.
        \li In the \uicontrol {Current settings} field, select the settings to
            modify and click \uicontrol Copy.
        \li Give a name to the settings and click \uicontrol OK.
        \li Click \uicontrol Edit to set
            \l{https://clang.llvm.org/docs/ClangFormatStyleOptions.html}
            {ClangFormat Style Options}.
    \endlist

    In the other tabs, 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.
        \li Name getter functions.
    \endlist

    Use the live preview to see how the preferences change the indentation.

    \section1 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}

    \section1 Specifying Settings for Braces

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

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

    \section1 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}

    \section1 Specifying Alignment

    To align continuation lines to tokens after assignments, such as \c = or
    \c +=, select the \uicontrol {Align after assignments} check box. 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}

    \section1 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 Project-Specific ClangFormat Files

    To override the \c {.clang-format} file for a particular project, create a
    copy of the built-in style and edit its settings by selecting
    \uicontrol Projects > \uicontrol {Project Settings} >
    \uicontrol {Code Style} > \uicontrol Copy > \uicontrol Edit >
    \uicontrol {ClangFormat} >
    \uicontrol {Override ClangFormat configuration file}.

    \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}, {Behavior}, {Qt Quick Code Style}, {Nim}
*/