1. QMetaMethod class
① Function: It is used to describe the member function of the object. The member function of this class can be used to obtain the information of the object member function.
② This class has the following members:
enum MethodType{Method, Signal, Slot, Constructor}
This enumeration is used to describe the type of function, namely: ordinary member function (Method), signal (Signal), slot (Slot), constructor
Constructor.
enum Access(Private, Protected, Public)
This enumeration is mainly used to describe the access level of the function (private, protected, public)
QByteArray methodSignature() const; //Returns the signature of the function (qt5.0)
MethodType methodType() const; //The type of the return function (signal, slot, member function, constructor)
QByteArray name() const; //The name of the return function (qt5.0)
int parameterCount() const; //Returns the number of function parameters (qt5.0)
QList<QByteArray> parameterNames() const; Returns a list of function parameter names.
int parameterType(int index) const;
returns the parameter type at the specified index. The return value is the type registered with QMetaType. If the type is not registered,
the return value is QMetaType::UnknownType.
QList<QByteArray> parameterTypes() const; Returns a list of function parameter types.
int returnType() const;
returns the return type of the function. The return value is the type registered with QMetaType. If the type is not registered, the return
value is QMetaType::UnknownType.
const char * typeName() const; Returns the name of the return type of the function.
Access access() const; returns the access level of the function (private, protected, public)
2. The functions of the QMetaObject class to obtain the member functions of the class object are:
①, int indexOfMethod(const char* f) const;
returns the index number of the function named f, otherwise it returns -1. The correct function signature should be entered here, such as the function shape
The formula is void f(int i,int j); the correct form is xx.indexOfMethod("f(int,int"); The following forms are not
correct forms, "f(int i, int j)", " void f(int, int)", "f", "void f", etc.
②, int indexOfSignal(const char * s) const;
returns the index number of the signal s, otherwise it returns -1, if the specified function exists, but It is not a signal, and still returns -1.
③, int indexOfConstructor(const char *c) const; //Returns the index number of the constructor c, otherwise it returns -1
④, int constructorCount() const; //Returns the number of constructors.
⑤, QMetaMethod constructor(int i)const; Returns the metadata of the constructor at the specified index i.
⑥, int methodCount() const;
Returns the number of functions, including functions, signals, slots and ordinary member functions in the base class.
⑦, int methodOffset() const;
returns the sum of all functions in the parent class, that is to say, the returned value is the
index position of the first member function in the class .
⑧, QMetaMethod method(int i) const; returns Specifies the metadata of the function at index i.
#ifndef M_H //要使用元对象系统,需在头文件中定义类。
#define M_H
#include<QObject> //因为要使用QObject 类,为此需要包含此头文件
class A:public QObject
{
Q_OBJECT //启动元对象系统,必须声明此宏
public:
//定义2 个构造函数、1 个信号、3 个函数。
Q_INVOKABLE A(){} //要想函数被反射,需要指定Q_INVOKABLE 宏。
Q_INVOKABLE A(int){}
Q_INVOKABLE void f(){}
Q_INVOKABLE void g(int i,float j){}
void g1(){} //注意:此函数不会被反射。
signals: void gb3();
};
class B:public A
{
Q_OBJECT //要使用元对象系统,应在每个类之中都声明此宏
public:
//定义1 个函数、2 个信号
Q_INVOKABLE void f(){}
signals: void gb4(); void gb5();
};
#endif // M_H
#include "m.h"
#include <QMetaMethod>
#include <QByteArray>
#include <iostream>
using namespace std;
int main()
{ A ma; B mb; //创建两个对象
const QMetaObject *pa=ma.metaObject();
const QMetaObject *pb=mb.metaObject();
//以下为通过QMetaObject 的成员函数获取的信息。
int j=pa->methodCount(); /*返回对象ma 中的成员函数数量,包括从父类QObject 继承而来的5 个
成员函数及本对象中的2 个成员函数(注意,不包括g1)、1 个信号,所以
总数为8。*/
cout<<j<<endl; //输出8
int i=pa->indexOfMethod("g(int,float)"); //获取对象ma 中的成员函数g 的索引号。
cout<<i<<endl; //输出7
i=pa->constructorCount(); //获取对象ma 所属类中的构造函数的数量。
cout<<i<<endl; //输出2
i=pb->constructorCount(); /*获取对象mb 所属类B 中的构造函数的数量,因类B 无构造函数,所以
返回值为0,此处也可看到,构造函数数量不包含父类的构造函数*/
cout<<i<<endl; //输出0。
i=pa->indexOfConstructor("A(int)"); //获取对象ma 所属类中的构造函数A(int)的索引号。
cout<<i<<endl; //输出1。
i=pa->indexOfSignal("gb3()"); //获取对象ma 的信号gb3()的索引号。
cout<<i<<endl; //输出5。
i=pa->indexOfSignal("f()"); /*获取对象ma 的信号f()的索引号。因为成员函数f 存在,但不是信
号,所以返回值为-1。*/
cout<<i<<endl; //输出-1。
i=pb->methodOffset(); /*获取父类的成员函数数量,包括父类A 及QObject 中的成员函数,总共为8。
*/
cout<<i<<endl; //输出8,此处相当于是对象mb 自身成员函数开始处的索引号。
//以下为通过QMetaMethon 的成员函数获取的信息。
//获取对象ma 的成员函数g 的元数据。
QMetaMethod m=pa->method(pa->indexOfMethod("g(int,float)"));
QByteArray s= m.name(); //获取成员函数g 的函数名。
cout<<s.data()<<endl; //输出g
s=m.methodSignature(); //获取函数g 的签名
cout<<s.data()<<endl; //输出g(int,float)
i=m.methodType(); /*获取函数g 的类型,此处返回的是QMetaMethod::MethodType 中定义的枚举值,
其中Method=0,表示类型为成员函数*/
cout<<i<<endl; //输出0(表示成员函数)。
//以下信息与函数的返回类型有关
s=m.typeName(); //获取函数g 的返回值的类型名
cout<<s.data()<<endl; //输出void
i=m.returnType(); /*获取函数g 返回值的类型,此处的类型是QMetaType 中定义的枚举值,其中枚举
值QMetaType::void=43*/
cout<<i<<endl; //输出43
//以下信息与函数的参数有关
i=m.parameterType(1); /*获取函数g 中索引号为1 的参数类型,此处的类型是QMetaType 中定义的
枚举值,其中枚举值QMetaType::float=38*/
cout<<i<<endl; //输出38
QList<QByteArray> q=m.parameterNames(); //获取函数g 的参数名列表
cout<<q[0].data()<<q[1].data()<<endl; //输出ij
q=m.parameterTypes(); //获取函数g 的参数类型列表。
cout<<q[0].data()<<q[1].data()<<endl; //输出intfloat
return 0;
}