Qt:QMetaMethod类

1059 篇文章 278 订阅

头文件:

#include <QMetaMethod>

cmake:

find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core)

qmake:

QT += core

详细介绍

QMetaMethod:获取成员函数的元信息

  • 通过该类,我们可以获取到一个成员函数的类型,比如它是信号、槽、方法还是构造函数。
  • 也可以获取方法的完整签名,以及方法所接受的参数类型和参数名字
  • 同样,可以获取方法的返回值类型
  • 还可以使用access()方法获取成员函数的访问权限。
  • 当然,最重要的还是invoke()方法,使用该方法我们可以在任意的QObject对象上调用成员函数

而要得到一个QMetaMethod类的实例,有如下方法:

  • 对于信号,可以使用该类的静态方法fromSignal()来获得相对于该信号的QMetaMethod对象;
QMetaMethod destroyedSignal = QMetaMethod::fromSignal(&QObject::destroyed);
  • 对于普通成员函数和槽函数,可以使用类的QMetaObject对象来间接获取。
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));

其中,要使用QMetaObject::normalizedSignature()来规范化函数签名,确保方法签名是invoke()所期望的。

成员类型

Access

enum QMetaMethod::Access

这个枚举遵循C++中使用的约定描述了一个方法的访问级别。

ConstantValue
QMetaMethod::Private0
QMetaMethod::Protected1
QMetaMethod::Public2

MethodType

enum QMetaMethod::MethodType

ConstantValue描述
QMetaMethod::Method0该成员函数是一个方法
QMetaMethod::Signal1该成员函数是一个信号
QMetaMethod::Slot2该成员函数是一个槽
QMetaMethod::Constructor3该成员函数是一个构造函数

成员函数

access

QMetaMethod::Access access() const

返回此方法的访问规范(private、protected或public)。

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

另请参见:methodType().

fromSignal

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

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

例子:

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

isValid

[since 5.0]
bool QMetaMethod::isValid() const

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

这个函数是在qt5.0中引入的。

methodIndex

int QMetaMethod::methodIndex() const

返回此方法的索引。

methodSignature

[since 5.0]
QByteArray QMetaMethod::methodSignature() const

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

这个函数是在qt5.0中引入的。

另请参见:parameterTypes() 、 parameterNames().

methodType

QMetaMethod::MethodType QMetaMethod::methodType() const

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

另请参见:access()

name

[since 5.0]
QByteArray QMetaMethod::name() const

返回此方法的名称。

这个函数是在qt5.0中引入的。

另请参见:methodSignature() 、 parameterCount().

parameterCount

[since 5.0]
int QMetaMethod::parameterCount() const

返回此方法的参数数。

这个函数是在qt5.0中引入的。
另请参见:parameterType() 、 parameterNames().

parameterMetaType

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

返回给定索引处参数的元类型。

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

这个函数是在qt6.0中引入的。

另请参见:parameterCount(), returnMetaType(), QMetaType.

parameterNames

QList<QByteArray> QMetaMethod::parameterNames() const

返回参数名列表。

另请参见:parameterTypes() , methodSignature().

parameterType

[since 5.0]
int QMetaMethod::parameterType(int index) const

返回给定索引处的参数类型。

返回值是使用QMetaType注册的类型之一,如果该类型未注册,则返回QMetaType::UnknownType。

这个函数是在qt5.0中引入的。

另请参见:parameterCount(), parameterMetaType(), returnType(), QMetaType.

parameterTypeName

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

返回位置索引处类型的名称,如果索引处没有参数,则返回空的QByteArray

这个函数是在qt6.0中引入的。

另请参见:parameterNames()

parameterTypes

QList< QByteArray > QMetaMethod::parameterTypes() const

返回参数类型的列表
另请参见:parameterNames() 、 methodSignature().

relativeMethodIndex

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

返回此方法的内部局部索引。

这个函数是在qt6.0中引入的。

returnMetaType

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

返回此方法的返回类型。

这个函数是在qt6.0中引入的。

另请参见:parameterMetaType(), QMetaType, typeName().

returnType

[since 5.0]
int QMetaMethod::returnType() const

返回此方法的返回类型。

返回值是使用QMetaType注册的类型之一,如果该类型未注册,则返回QMetaType::UnknownType。

这个函数是在qt5.0中引入的。

另请参见:parameterType(), QMetaType, typeName(), returnMetaType().

revision

[since 5.1]
int QMetaMethod::revision() const

如果Q_REVISION指定了一个方法修订版,则返回方法修订版,否则返回0。

此功能在Qt 5.1中引入。

tag

const char *QMetaMethod::tag() const

返回与此方法关联的tag。

Tags 是由moc识别的特殊宏,可以添加有关方法的额外信息。

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

    // 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();

    int functionIndex = win.metaObject()->indexOfSlot("testFunc()");
    QMetaMethod mm = win.metaObject()->method(functionIndex);
    qDebug() << mm.tag(); // prints MY_CUSTOM_TAG

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

注意:由于qt5.0,moc扩展了预处理器宏,因此有必要用#ifndef Q_MOC_RUN包围定义,如上面的示例所示。这在Qt 4中不是必需的。上面显示的代码也适用于qt4。

typeName

const char *QMetaMethod::typeName() const

返回此方法的返回类型名称。

另请参见returnType() 、 QMetaType::type().

例子:


     QByteArray normalizedSignature = QMetaObject::normalizedSignature("cirleSolve(int)");
     int methodIndex = obj.metaObject()->indexOfMethod(normalizedSignature);
     QMetaMethod metaMethod = obj.metaObject()->method(methodIndex);
     qDebug() << "Access: " << metaMethod.access();
     qDebug() << "Valid: " << metaMethod.isValid();
     qDebug() << "Index: " << metaMethod.methodIndex();
     qDebug() << "Signature: " << metaMethod.methodSignature();
     qDebug() << "Type: " << metaMethod.methodType();
     qDebug() << "Name: " << metaMethod.name();
     qDebug() << "Parameter names: " << metaMethod.parameterNames();
     qDebug() << "Parameter types: " << metaMethod.parameterTypes();
     qDebug() << "Return type: " << metaMethod.returnType();
     qDebug() << "Type name: " << metaMethod.typeName();

     //int result = 0;
     bool bCall = metaMethod.invoke(&obj,  Qt:: AutoConnection,Q_ARG(int, 42));
     if(bCall)
     {
       qDebug() << "the area of obj " << obj.getArea();
     }

输出:
在这里插入图片描述
可以看到,QMetaObject对象obj 获取到了槽函数的相关信息,并且通过invoke ()函数实现了对 槽函数circleSolve(int)的调用,将“”42”的值传入,并进行计算。
QMetaMethod 获取成员函数的元信息

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值