QMetaMethod Class

QMetaMethod 类提供有关成员函数的元数据。更多

头文件: #include <QMetaMethod>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

该类可等价比较

公共类型

enum Access { Private, Protected, Public }
enum MethodType { Method, Signal, Slot, Constructor }

公共函数

QMetaMethod::Access access() const
(since 6.5) bool invoke(QObject *obj, Args &&... arguments) const
(since 6.5) bool invoke(QObject *obj, QTemplatedMetaMethodReturnArgument<ReturnArg> ret, Args &&... arguments) const
(since 6.5) bool invoke(QObject *obj, Qt::ConnectionType type, Args &&... arguments) const
(since 6.5) bool invoke(QObject *obj, Qt::ConnectionType type, QTemplatedMetaMethodReturnArgument<ReturnArg> ret, Args &&... arguments) const
(since 6.5) bool invokeOnGadget(void *gadget, Args &&... arguments) const
(since 6.5) bool invokeOnGadget(void *gadget, QTemplatedMetaMethodReturnArgument<ReturnArg> ret, Args &&... arguments) const
(since 6.2) bool isConst() const
bool isValid() const
int methodIndex() const
QByteArray methodSignature() const
QMetaMethod::MethodType methodType() const
QByteArray name() const
(since 6.9) QByteArrayView nameView() const
int parameterCount() const
(since 6.0) QMetaType parameterMetaType(int index) const
QList<QByteArray> parameterNames() const
int parameterType(int index) const
(since 6.0) QByteArray parameterTypeName(int index) const
QList<QByteArray> parameterTypes() const
(since 6.0) int relativeMethodIndex() const
(since 6.0) QMetaType returnMetaType() const
int returnType() const
int revision() const
const char *tag() const
const char *typeName() const

静态公共成员

QMetaMethod fromSignal(PointerToMemberFunction signal)
bool operator!=(const QMetaMethod &lhs, const QMetaMethod &rhs)
bool operator==(const QMetaMethod &lhs, const QMetaMethod &rhs)

详细描述

一个 QMetaMethod 有一个methodType() 、一个methodSignature() 、一个parameterTypes() 和parameterNames() 的列表、一个返回typeName() 、一个tag() 和一个access() 指定符。您可以使用invoke() 在任意QObject 上调用该方法。

另请参阅 QMetaObject,QMetaEnum,QMetaProperty, 以及Qt XML 的属性系统

成员类型文档

enum QMetaMethod::Access

该枚举按照 C++ 的约定描述了方法的访问级别。

常量
QMetaMethod::Private0
QMetaMethod::Protected1
QMetaMethod::Public2

enum QMetaMethod::MethodType

常数说明
QMetaMethod::Method0该函数是一个普通成员函数。
QMetaMethod::Signal1该函数是一个信号。
QMetaMethod::Slot2该函数是一个槽。
QMetaMethod::Constructor3该函数是一个构造函数。

成员函数文档

[since 6.5] template <typename... Args> bool QMetaMethod::invoke(QObject *obj, Args &&... arguments) const

[since 6.5] template <typename ReturnArg, typename... Args> bool QMetaMethod::invoke(QObject *obj, QTemplatedMetaMethodReturnArgument<ReturnArg> ret, Args &&... arguments) const

[since 6.5] template <typename... Args> bool QMetaMethod::invoke(QObject *obj, Qt::ConnectionType type, Args &&... arguments) const

[since 6.5] template <typename ReturnArg, typename... Args> bool QMetaMethod::invoke(QObject *obj, Qt::ConnectionType type, QTemplatedMetaMethodReturnArgument<ReturnArg> ret, Args &&... arguments) const

在对象object 上调用此方法。如果可以调用该成员,则返回true 。如果没有此类成员或参数不匹配,则返回false

对于带有 QTemplatedMetaMethodReturnArgument 参数的重载,member 函数调用的返回值将放在ret 中。对于没有此类成员的重载,被调用函数的返回值(如果有)将被丢弃。QTemplatedMetaMethodReturnArgument 是一种内部类型,不应直接使用。请使用 qReturnArg() 函数。

带有Qt::ConnectionType type 参数的重载允许明确选择调用是否同步:

  • 如果typeQt::DirectConnection ,则将在当前线程中立即调用该成员。
  • 如果typeQt::QueuedConnection ,则将发送QEvent ,一旦应用程序进入obj 创建或移动到的线程的事件循环,就会调用该成员。
  • 如果typeQt::BlockingQueuedConnection ,方法的调用方式将与Qt::QueuedConnection 相同,只是当前线程将阻塞,直到事件发送完毕。使用这种连接类型在同一线程中的对象之间进行通信会导致死锁。
  • 如果typeQt::AutoConnection ,如果obj 与调用者位于同一线程,则会同步调用该成员;否则会异步调用该成员。这是没有type 参数的重载的行为。

QPushButton 上异步调用animateClick() 槽:

int methodIndex = pushButton->metaObject()->indexOfMethod("animateClick()");
QMetaMethod method = metaObject->method(methodIndex);
method.invoke(pushButton, Qt::QueuedConnection);

在异步方法调用中,参数必须是可复制类型,因为 Qt 需要复制参数并将其存储在幕后事件中。自 Qt 6.5 起,该函数会自动注册所使用的类型;但其副作用是,无法使用仅向前声明的类型进行调用。此外,异步调用也不能使用对非定式类型的引用作为参数。

要在某个任意对象上同步调用compute(QString, int, double) 槽,obj 获取其返回值:

QString retVal;
QByteArray normalizedSignature = QMetaObject::normalizedSignature("compute(QString, int, double)");
int methodIndex = obj->metaObject()->indexOfMethod(normalizedSignature);
QMetaMethod method = obj->metaObject()->method(methodIndex);
method.invoke(obj, Qt::DirectConnection, qReturnArg(retVal),
              QString("sqrt"), 42, 9.7);

如果 "计算 "槽没有按照指定的顺序接收一个QString 、一个int 和一个double ,调用将失败。请注意有必要明确说明QString 的类型,因为字符字面量并不是匹配的正确类型。如果该方法使用的是QByteArrayqint64long double ,则需要将调用写成:

QString retVal;
QByteArray normalizedSignature = QMetaObject::normalizedSignature("compute(QByteArray, qint64, long double)");
int methodIndex = obj->metaObject()->indexOfMethod(normalizedSignature);
QMetaMethod method = obj->metaObject()->method(methodIndex);
method.invoke(obj, Qt::DirectConnection, qReturnArg(retVal),
              QByteArray("sqrt"), qint64(42), 9.7L);

同样的调用可以使用Q_ARG() 和Q_RETURN_ARG() 宏来执行,如下所示:

QString retVal;
QByteArray normalizedSignature = QMetaObject::normalizedSignature("compute(QString, int, double)");
int methodIndex = obj->metaObject()->indexOfMethod(normalizedSignature);
QMetaMethod method = obj->metaObject()->method(methodIndex);
method.invoke(obj,
              Qt::DirectConnection,
              Q_RETURN_ARG(QString, retVal),
              Q_ARG(QString, "sqrt"),
              Q_ARG(int, 42),
              Q_ARG(double, 9.7));

警告: 此方法不会测试参数的有效性:object 必须是QMetaObject 类的实例,此QMetaMethod 是用该类构建的。

此函数在 Qt 6.5 中引入。

另请参阅 Q_ARG()、Q_RETURN_ARG()、qRegisterMetaType() 和QMetaObject::invokeMethod()。

[since 6.5] template <typename... Args> bool QMetaMethod::invokeOnGadget(void *gadget, Args &&... arguments) const

[since 6.5] template <typename ReturnArg, typename... Args> bool QMetaMethod::invokeOnGadget(void *gadget, QTemplatedMetaMethodReturnArgument<ReturnArg> ret, Args &&... arguments) const

Q_GADGET 上调用该方法。如果可以调用该成员,则返回true 。如果没有此类成员或参数不匹配,则返回false

指针gadget 必须指向小工具类的实例。

调用始终是同步的。

对于带有 QTemplatedMetaMethodReturnArgument 参数的重载,member 函数调用的返回值将放在ret 中。对于不带该参数的重载,被调用函数的返回值(如果有)将被丢弃。QTemplatedMetaMethodReturnArgument 是一种内部类型,不应直接使用。请使用 qReturnArg() 函数。

警告: 该方法不会测试参数的有效性:gadget 必须是该QMetaMethod 所构造的QMetaObject 类的实例。

此函数在 Qt 6.5 中引入。

另请参阅 Q_ARG()、Q_RETURN_ARG()、qRegisterMetaType() 和QMetaObject::invokeMethod()。

QMetaMethod::Access QMetaMethod::access() const

返回此方法的访问规范(私有、受保护或公用)。

注意: 信号总是公用的,但应将其视为实现细节。从类外发射信号几乎总是一个坏主意。

另请参见 methodType()。

[static] template <typename PointerToMemberFunction> QMetaMethod QMetaMethod::fromSignal(PointerToMemberFunction signal)

返回与给定的signal 对应的元方法,如果signalnullptr 或不是该类的信号,则返回无效的QMetaMethod

示例

QMetaMethod destroyedSignal = QMetaMethod::fromSignal(&QObject::destroyed);

[since 6.2] bool QMetaMethod::isConst() const

返回方法是否为 const 限定。

注意: 如果属于根据旧版本 Qt 编译的库,此方法可能会错误地返回 const 方法的false

此函数在 Qt 6.2 中引入。

bool QMetaMethod::isValid() const

如果该方法有效(可以反省和调用),则返回true ,否则返回false

int QMetaMethod::methodIndex() const

返回此方法的索引。

QByteArray QMetaMethod::methodSignature() const

返回此方法的签名(如setValue(double) )。

另请参阅 parameterTypes() 和parameterNames()。

QMetaMethod::MethodType QMetaMethod::methodType() const

返回此方法的类型(信号、槽或方法)。

另请参阅 access().

QByteArray QMetaMethod::name() const

返回此方法的名称。

另请参阅 methodSignature() 和parameterCount()。

[since 6.9] QByteArrayView QMetaMethod::nameView() const

返回该方法的名称。只要方法所属类的元对象有效,返回的QByteArrayView 就有效。

此函数在 Qt 6.9 中引入。

另请参阅 name

int QMetaMethod::parameterCount() const

返回此方法的参数数。

另请参阅 parameterType() 和parameterNames()。

[since 6.0] QMetaType QMetaMethod::parameterMetaType(int index) const

返回给定index 中参数的元类型。

如果index 小于零或大于parameterCount() ,则返回无效的QMetaType

此函数在 Qt 6.0 中引入。

另请参阅 parameterCount()、returnMetaType() 和QMetaType

QList<QByteArray> QMetaMethod::parameterNames() const

返回参数名称列表。

另请参阅 parameterTypes() 和methodSignature()。

int QMetaMethod::parameterType(int index) const

返回给定index 的参数类型。

返回值是在QMetaType 注册的类型之一,如果未注册类型,则返回QMetaType::UnknownType

另请参阅 parameterCount(),parameterMetaType(),returnType() 和QMetaType

[since 6.0] QByteArray QMetaMethod::parameterTypeName(int index) const

返回index 位置的类型名称。如果index 位置没有参数,则返回空值。QByteArray

此函数在 Qt 6.0 中引入。

另请参阅 parameterNames() 。

QList<QByteArray> QMetaMethod::parameterTypes() const

返回参数类型列表。

另请参阅 parameterNames() 和methodSignature()。

[since 6.0] int QMetaMethod::relativeMethodIndex() const

返回此方法内部的本地索引。

此函数在 Qt 6.0 中引入。

[since 6.0] QMetaType QMetaMethod::returnMetaType() const

返回此方法的返回类型。

此函数在 Qt 6.0 中引入。

另请参阅 parameterMetaType(),QMetaType, 和typeName().

int QMetaMethod::returnType() const

返回此方法的返回类型。

返回值是QMetaType 注册的类型之一,如果未注册类型,则为QMetaType::UnknownType

另请参阅 parameterType(),QMetaType,typeName() 和returnMetaType() 。

int QMetaMethod::revision() const

如果Q_REVISION 指定了方法修订,则返回方法修订,否则返回 0。自 Qt 6.0 起,非零值被编码,可使用QTypeRevision::fromEncodedVersion() 解码。

const char *QMetaMethod::tag() const

返回与此方法相关的标记。

标签是moc 可识别的特殊宏,可用于添加方法的额外信息。

标签信息可以通过以下方式添加到函数声明中:

    // In the class MainWindow declaration
    #ifndef Q_MOC_RUN
    // define the tag text as empty, so the compiler doesn't see it
    #  define MY_CUSTOM_TAG
    #endif
    ...
    private slots:
        MY_CUSTOM_TAG void testFunc();

并通过使用

    MainWindow win; win.show();intfunctionIndex=win.metaObject()->indexOfSlot("testFunc()");QMetaMethodmm=win.metaObject()->method(functionIndex);    qDebug() << mm.tag(); // prints MY_CUSTOM_TAG

目前,moc 将提取并记录所有标记,但不会对其中任何标记进行特殊处理。您可以使用这些标记对您的方法进行不同的注释,并根据您应用程序的具体需要进行处理。

注意: moc 会扩展预处理器宏,因此有必要在定义周围加上#ifndef Q_MOC_RUN ,如上例所示。

const char *QMetaMethod::typeName() const

返回此方法的返回类型名称。如果该方法是一个构造函数,该函数将返回空字符串(构造函数没有返回类型)。

注: 在 Qt 7 中,此函数将为构造函数返回空指针。

另请参阅 returnType() 和QMetaType::type()。

相关非成员

[noexcept] bool operator!=(const QMetaMethod &lhs, const QMetaMethod &rhs)

这是一个重载函数。

如果方法lhs 不等于方法rhs ,则返回true ,否则返回false

[noexcept] bool operator==(const QMetaMethod &lhs, const QMetaMethod &rhs)

这是一个重载函数。

如果方法lhs 等于方法rhs ,则返回true ,否则返回false

宏文档

Q_METAMETHOD_INVOKE_MAX_ARGS

等于通过QMetaMethod::invoke() 执行方法时可使用的最大参数数

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