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.

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.

For documenting command-line instructions, shell scripts, or any content that is not in a Qt language recognized by QDoc, use \badcode instead.

When processing any of the \code, \newcode or \oldcode commands, 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
* /

QDoc renders this as:

#include <QApplication>
#include <QPushButton>

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

Other QDoc commands are disabled within \code... \endcode, and the special character '\' is accepted and rendered like the rest of the code.

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

See also \c, \badcode, \quotefromfile, \newcode, and \oldcode.

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

Substitute \code with this command when documenting command-line instructions, shell scripts or any other content that is not in a Qt language, but should still be styled similarly to a \code paragraph.

\newcode

The \newcode, \oldcode, and \endcode commands enable you to show how to port a snippet of code to a new version of an API.

The \newcode command and its companion the \oldcode command are a convenience combination of the \code commands: this combination provides a text relating the two code snippets to each other.

The \newcode command requires a preceding \oldcode statement.

Like the \code command, the \newcode command renders its code on a new line in the documentation using a monospace font and the standard indentation.

/ *!
    \oldcode
        if (printer->setup(parent))
            ...
    \newcode
        QPrintDialog dialog(printer, parent);
            if (dialog.exec())
                ...
    \ endcode
* /

QDoc renders this as:

For example, if you have code like

if (printer->setup(parent))
    ...

you can rewrite it as

QPrintDialog dialog(printer, parent);
    if (dialog.exec())
        ...

Other QDoc commands are disabled within \oldcode ... \endcode, and the '\' character doesn't need to be escaped.

\oldcode

The \oldcode command requires a corresponding \newcode statement; otherwise QDoc fails to parse the command and emits a warning.

See also \newcode.

\qml

The \qml and \endqml commands enclose a snippet of QML source code.

/ *!
    \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
* /

QDoc renders this as:

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

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