On this page

Including Code Inline

The following commands are used to render source code without formatting. The source code begins on a new line, rendered in the code.

Note: Although most of these commands are for rendering C++ code, the \snippet and \codeline commands are preferred over the others. These commands allow equivalent code snippets for other Qt language bindings to be substituted for the C++ snippets in the documentation.

\code

The \code and \endcode commands enclose a snippet of source code. The \code command accepts two kinds of optional arguments on the same line as the command: a language in square brackets, and a list of snippet parameters:

\code [language] parameters

The language argument controls highlighting and linking, as described in Highlighting and autolinking. The parameters are strings that QDoc injects into the snippet, as described in Code snippet parameters.

Note: The \c command can be used for short code fragments within a sentence. The \code command is for longer code snippets. It renders the code verbatim in a separate paragraph in a html <pre> element, and parses the enclosed snippet, creating links to any known types in the code.

QDoc highlights and creates links for C++ and QML code only. For command-line instructions, shell scripts, or code in any other language, use \code [text] to render the content as plain text. Prefer \code [text] over the older \badcode command for this purpose.

When processing the \code command, QDoc removes all indentation that is common for the verbatim code blocks within a /*! ... */ comment before it adds the standard indentation.

Note: This doesn't apply to externally quoted code using the \quotefromfile or \quotefile command.

/*!
    \code
        #include <QApplication>
        #include <QPushButton>

        int main(int argc, char *argv[])
        {
            ...
        }
    \endcode
*/

Other QDoc commands are disabled within \code... \endcode, and the special character '\' is accepted and rendered like the rest of the code, unless it is followed by a digit and parameters were passed to \code.

Highlighting and autolinking

The \code commands attempts to parse its contents as code of a specific language, as defined in the language configuration variable. This provides highlighting and automatic linking to types detected in the code.

As an exception since QDoc version 6.4, when the \code command is used within a QML-specific topic, QDoc first attempts to recognize the code as QML; for other topics, the language configuration variable takes precedence. To explicitly mark the code snippet as QML, use the \qml command instead.

From version 6.11, a language can be specified as an optional case-insensitive argument on the same line as the \code command. When specified, the language overrides the default language and any other language-specific behavior described above. QDoc recognizes the following language arguments:

ArgumentEffect
cpp, cHighlights the snippet as C++ and creates links to known types.
qmlHighlights the snippet as QML and creates links to known types.
textRenders the snippet as plain text, without highlighting or links.
A language listed in the codelanguages configuration variableRenders the snippet without highlighting or links, but records the language in the generated HTML.

For any other language argument, QDoc warns about an unrecognized markup language and handles the snippet as if no language was given.

For example:

\code [text]
    # This is an example of unmarked code.
    implement MyModule;
    include "sys.m";
    sys: Sys;
\endcode

This can be useful when quoting a language that QDoc does not have the ability to mark up.

To let other tools highlight such code, add its language to the codelanguages configuration variable and pass that language to \code instead of text. QDoc includes the language as metadata in the generated HTML.

Code snippet parameters

Since QDoc version 5.12, \code command accepts also optional parameters. Parameters are useful for injecting simple strings into the code snippet. To inject a string to a specific location in the snippet, add a backslash followed by a digit (1..8). The digits correspond with the order of the argument list, where arguments are separated by spaces and follow any optional language argument.

For example:

/*!
    \code * hello
    /\1 \2 \1/
    \endcode
*/

For the above snippet, QDoc renders the word hello enclosed in a C-style comment.

Including code from external files

To include code snippets from an external file, use the \snippet and \codeline commands.

See also \c, \qml, \badcode, \quotefromfile, and language.

\badcode

Similar to \code, \badcode and \endcode commands enclose content that is rendered verbatim in a separate paragraph, but no parsing or automatic link creation is performed. Instead, the content is treated as plain text, the same way as with \code [text].

The \badcode command predates the language argument of \code. For command-line instructions, shell scripts, or any other content that QDoc can't highlight, use \code [text] instead.

Like \code, \badcode accepts optional snippet parameters. Unlike \code, it doesn't accept a language argument: all text on the same line as \badcode is read as snippet parameters. To highlight a snippet as C++ or QML, use \code [cpp] or \code [qml] instead.

\qml

The \qml and \endqml commands enclose a snippet of QML source code. Use these for proper syntax highlighting of QML code snippets. The enclosed snippet must be complete as if it was a valid .qml file. If the snippet is incomplete, QDoc will issue a warning and ignore the snippet.

/*!
    \qml
        import QtQuick 2.0

        Row {
            Rectangle {
                width: 100; height: 100
                color: "blue"
                transform: Translate { y: 20 }
            }
            Rectangle {
                width: 100; height: 100
                color: "red"
                transform: Translate { y: -20 }
            }
        }
    \endqml
*/

Like the \code command, \qml accepts optional parameters.

© 2026 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.