QMetaMethod Class
QMetaMethod 类提供了有关成员函数的元数据。更多内容...
| 头文件: | #include <QMetaMethod> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
QMetaMethod 比较
| 类别 | 可比较类型 |
|---|---|
| equality | QMetaMethod |
公共类型
| 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) |
宏
(until 6.13) | Q_METAMETHOD_INVOKE_MAX_ARGS |
详细说明
一个 QMetaMethod 具有一个methodType()、一个methodSignature()、一组parameterTypes() 和parameterNames()、一个返回typeName()、一个tag() 以及一个access() 指定符。您可以使用invoke() 在任意QObject 上调用该方法。
另请参阅 QMetaObject 、QMetaEnum 、QMetaProperty 以及Qt 的属性系统。
成员类型文档
enum QMetaMethod::Access
该枚举描述了方法的访问级别,遵循 C++ 中的约定。
| 常量 | 值 |
|---|---|
QMetaMethod::Private | 0 |
QMetaMethod::Protected | 1 |
QMetaMethod::Public | 2 |
enum QMetaMethod::MethodType
| 常数 | 值 | 描述 |
|---|---|---|
QMetaMethod::Method | QMETHOD_CODE | 该函数是一个普通成员函数。 |
QMetaMethod::Signal | 1 | 该函数是一个信号。 |
QMetaMethod::Slot | 2 | 该函数是一个槽。 |
QMetaMethod::Constructor | 3 | 该函数是一个构造函数。 |
成员函数文档
QMetaMethod::Access QMetaMethod::access() const
返回此方法的访问修饰符(private、protected 或 public)。
注意:信号 始终是公有的,但您应将其视为实现细节。从类外部发出信号几乎总是一个坏主意。
另请参阅 methodType()。
[static] template <typename PointerToMemberFunction> QMetaMethod QMetaMethod::fromSignal(PointerToMemberFunction signal)
返回与给定的signal 相对应的元方法;如果signal 的值是nullptr ,或者该信号不属于该类,则返回无效的QMetaMethod 。
示例:
QMetaMethod destroyedSignal = QMetaMethod::fromSignal(&QObject::destroyed);[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 参数的重载允许显式选择调用是否为同步:
- 若type 为Qt::DirectConnection ,则该成员将立即在当前线程中被调用。
- 如果type 为Qt::QueuedConnection ,则会发送一个QEvent ,并在应用程序进入obj 被创建或移动到的线程中的事件循环时立即调用该成员。
- 如果type 为Qt::BlockingQueuedConnection ,则该方法的调用方式与Qt::QueuedConnection 相同,不同之处在于当前线程将阻塞直至事件被传递。使用此连接类型在同一线程内的对象之间进行通信将导致死锁。
- 如果 `type ` 为 `Qt::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 起,该函数会自动注册所使用的类型;但作为副作用,无法使用仅被前向声明的类型进行调用。此外,也无法进行使用指向未带 const 修饰符的类型的引用作为参数的异步调用。
若要对某个任意对象obj 同步调用compute(QString, int, double) 槽函数并获取其返回值,请按以下方式操作:
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);如果“compute”槽方法接收的参数并非恰好按指定顺序包含一个QString 、一个int 和一个double ,则调用将失败。 请注意,必须显式指定QString 的类型,因为字符字面量并非完全匹配的正确类型。如果该方法接受的参数分别是QByteArray 、qint64 和long 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 必须指向 gadget 类的实例。
调用始终是同步的。
对于带 QTemplatedMetaMethodReturnArgument 参数的重载,member 函数调用的返回值将被存入ret 中。对于不带该参数的重载,被调用函数的返回值(如有)将被丢弃。 QTemplatedMetaMethodReturnArgument 是一种内部类型,不应直接使用。应改用 qReturnArg() 函数。
警告:此方法不会 验证参数的有效性:gadget 必须是QMetaObject 所属类的实例,而该QMetaMethod 正是基于该 构造的。
这些函数在 Qt 6.5 中引入。
另请参阅 Q_ARG()、Q_RETURN_ARG()、qRegisterMetaType() 以及QMetaObject::invokeMethod()。
[since 6.2] bool QMetaMethod::isConst() const
返回该方法是否带有 const 修饰符。
注意: 如果该 const 方法所属的库是针对旧版本 Qt 编译的,则此方法 可能会错误地返回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()");
QMetaMethod mm=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::name()。
相关的非成员
[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 。
宏文档
[until 6.13] Q_METAMETHOD_INVOKE_MAX_ARGS
该宏计划在 6.13 版本中被废弃。
该数值等于通过旧版 `QMetaMethod::invoke()` 实现执行该方法时可接受的参数最大数量。新版实现(自 Qt 6.5 起)使用可变参数,且没有上限。
© 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.