本页内容

关联项目

关联命令用于指定一个文档元素与另一个文档元素之间的关联关系。例如:

  • 该函数是另一个函数的重载。
  • 该函数是另一个函数的重新实现。
  • 此 typedef 与某个类或头文件相关。

此外,还有一条命令用于记录某个 QML 类型继承了另一个 QML 类型。

命令

\inherits

\inherits 命令用于记录某个QML类型继承了另一个QML类型。该命令必须包含在继承元素的 \qmltype 注释中。其参数为被继承的 QML 类型的名称,可选地附加 QML 模块名称。

/*!
    \qmltype PauseAnimation
    \inqmlmodule QtQuick
    \nativetype QDeclarativePauseAnimation
    \ingroup qml-animation-transition
    \since 4.7
    \inherits Animation
    \brief The PauseAnimation element provides a pause for an animation.

    When used in a SequentialAnimation, PauseAnimation is a step
    when nothing happens, for a specified duration.

    A 500ms animation sequence, with a 100ms pause between two animations:

    SequentialAnimation {
        NumberAnimation { ... duration: 200 }
        PauseAnimation { duration: 100 }
        NumberAnimation { ... duration: 200 }
    }

    \sa {QML Animation and Transitions}, {declarative/animation/basics}{Animation basics example}
*/

QDoc 在PauseAnimation 元素的参考页面中包含了以下内容:

继承自Animation

在 .qml 文件中直接文档化 QML 类型时,通常无需使用 `\inherits ` 命令,因为 QDoc 可以从 QML 语法中检测到基类。如果存在 `\inherits ` 命令,则会覆盖这种自动基类检测。

\overload

请使用 \overload 命令来标记 C++ 函数重载。该命令必须在文档注释中单独成行。

工作原理

当您有多个同名 C++ 函数(重载)以不同的参数执行类似操作时,请使用 \overload 以避免文档内容重复。未标注 \overload 需要完整的文档说明。带有 \overload 的函数则可着重说明其具体差异。

标记为 \overload 会自动抑制参数缺失的警告,因为它们引用了主函数的文档。

基本用法

/*!
    \overload
    Brief description of what makes this overload different.
*/

链接到主函数

添加函数名称以创建指向主函数的链接:

/*!
    \overload functionName()
    Brief description of what makes this overload different.
*/

可使用限定名(ClassName::functionName() )或非限定名(functionName() )。QDoc 会自动使用当前类或命名空间对非限定名进行限定。

注意:出于 历史原因,类似functionName() 的无参数未限定名称是链接到主要重载的简写形式,并不一定指向无参数重载。 QDoc 使用一种搜索算法来查找应链接到的“最佳”重载。若要链接到特定的无参函数,请使用\overload primary 将其指定为主要重载,或者使用完全限定的签名并显式指定空参数列表。

指定主要重载

默认情况下,QDoc 会自动选择主要重载。若要显式指定哪个重载作为主要重载,请使用:

/*!
    \overload primary
    Main documentation for this function family.
    Document all parameters here.
*/

主重载:

  • 包含 main 函数的文档。
  • 要求提供完整的参数文档。
  • 不要显示“此函数重载了...”的文本。
  • 作为其他重载的链接目标。

当最重要的重载与QDoc的自动选择不一致时,或者当您需要保持链接行为的一致性时,请优先使用\overload 。

\reimp

\reimp 命令用于表明某个函数是虚拟函数的重写,而无需提供任何额外文档。

默认情况下,除非已编写文档说明,否则 QDoc 会将重写的虚拟函数从类参考中省略。此命令可确保原本未编写文档的函数被包含在内。

该命令必须单独成行。

/*!
    \reimp
*/
void QToolButton::nextCheckState()
{
    Q_D(QToolButton);
    if (!d->defaultAction)
        QAbstractButton::nextCheckState();
    else
        d->defaultAction->trigger();
}

该函数不会被纳入文档。取而代之的是,文档中将显示指向基类函数QAbstractButton::nextCheckState() 的链接。

\relates

\relates 命令用于将某个实体(函数、宏、typedef、枚举或变量)的文档包含到类、命名空间或头文件中。其参数是该实体所关联的类、命名空间或头文件的名称。

如果参数指向模板类型,请仅使用类型名(不带模板参数)。

/*!
    \relates QChar

    Reads a char from the stream \a in into char \a chr.

    \sa {Format of the QDataStream operators}
*/
QDataStream &operator>>(QDataStream &in, QChar &chr)
{
    quint16 u;
    in >> u;
    chr.unicode() = ushort(u);
    return in;
}

该函数的文档包含在类QChar 的参考页面中,列于“相关非成员”部分下。

注意:当 QDoc 在文档中解析未限定的函数或类型名称时 ,它会搜索文档所在页面的上下文。由于 `\relates ` 会将文档移动到另一个类的页面,因此在原始上下文中有效的自动链接可能无法再解析。若要交叉引用原始类的成员,请使用完全限定的名称或显式的 `\l ` 命令。

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