6. Qt 信号与信号槽(3)-QMetaObject

it2025-07-07  10

QMetaObject包含了QObject的元数据,也就是QObject信息的一些描述信息:除了类型信息外, signal&slot信息,Property等。数据都保存在d指针里面( QMetaObject.h 最后有声明d指针变量)。

QMetaObject.h

struct Q_CORE_EXPORT QMetaObject { class Connection; const char *className() const; const QMetaObject *superClass() const; bool inherits(const QMetaObject *metaObject) const Q_DECL_NOEXCEPT; QObject *cast(QObject *obj) const; const QObject *cast(const QObject *obj) const; #if !defined(QT_NO_TRANSLATION) || defined(Q_CLANG_QDOC) QString tr(const char *s, const char *c, int n = -1) const; #endif // QT_NO_TRANSLATION int methodOffset() const; int enumeratorOffset() const; int propertyOffset() const; int classInfoOffset() const; int constructorCount() const; int methodCount() const; int enumeratorCount() const; int propertyCount() const; int classInfoCount() const; int indexOfConstructor(const char *constructor) const; int indexOfMethod(const char *method) const; int indexOfSignal(const char *signal) const; int indexOfSlot(const char *slot) const; int indexOfEnumerator(const char *name) const; int indexOfProperty(const char *name) const; int indexOfClassInfo(const char *name) const; QMetaMethod constructor(int index) const; QMetaMethod method(int index) const; QMetaEnum enumerator(int index) const; QMetaProperty property(int index) const; QMetaClassInfo classInfo(int index) const; QMetaProperty userProperty() const; static bool checkConnectArgs(const char *signal, const char *method); // internal index-based connect static Connection connect(const QObject *sender, int signal_index, const QObject *receiver, int method_index, int type = 0, int *types = nullptr); // internal index-based disconnect static bool disconnect(const QObject *sender, int signal_index, const QObject *receiver, int method_index); static void connectSlotsByName(QObject *o); // internal index-based signal activation static void activate(QObject *sender, int signal_index, void **argv); static void activate(QObject *sender, const QMetaObject *, int local_signal_index, void **argv); static void activate(QObject *sender, int signal_offset, int local_signal_index, void **argv); static inline bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericArgument val0 = QGenericArgument(nullptr), QGenericArgument val1 = QGenericArgument(), // invokeMethod() for member function pointer template <typename Func> static typename std::enable_if<QtPrivate::FunctionPointer<Func>::IsPointerToMemberFunction && !std::is_convertible<Func, const char*>::value && QtPrivate::FunctionPointer<Func>::ArgumentCount == 0, bool>::type invokeMethod(typename QtPrivate::FunctionPointer<Func>::Object *object, Func function, Qt::ConnectionType type = Qt::AutoConnection, typename QtPrivate::FunctionPointer<Func>::ReturnType *ret = nullptr) { return invokeMethodImpl(object, new QtPrivate::QSlotObjectWithNoArgs<Func>(function), type, ret); } #endif QObject *newInstance(QGenericArgument val0 = QGenericArgument(nullptr), enum Call { InvokeMetaMethod, ReadProperty, WriteProperty, ResetProperty, QueryPropertyDesignable, QueryPropertyScriptable, QueryPropertyStored, QueryPropertyEditable, QueryPropertyUser, CreateInstance, IndexOfMethod, RegisterPropertyMetaType, RegisterMethodArgumentMetaType }; int static_metacall(Call, int, void **) const; static int metacall(QObject *, Call, int, void **); struct { // private data const QMetaObject *superdata;//父类的staticMetaObject指针 const QByteArrayData *stringdata;// 字符串数组,保存类的 类名,槽函数名 信号函数名等 字符串信息。 const uint *data;//该数组是个预定义的复合数据结构,由QMetaObjectPrivate 类提供管理,保存了类的基本信息,和一些索引值 typedef void (*StaticMetacallFunction)(QObject *, QMetaObject::Call, int, void **); StaticMetacallFunction static_metacall; const QMetaObject * const *relatedMetaObjects; void *extradata; //reserved for future use } d; private: static bool invokeMethodImpl(QObject *object, QtPrivate::QSlotObjectBase *slot, Qt::ConnectionType type, void *ret); };
最新回复(0)