类定义:
成员变量就只有QMetaObject *mobj和uint handle,handle同样用于计算在qt_meta_stringdata_XTest的位置
成员函数:
接下以test类进行函数讲解
test.h
#pragma once
#include <qobject.h>
#include <QFlags>
class XTest : public QObject
{
Q_OBJECT
Q_CLASSINFO("author", "Sabrina Schweinsteiger")
Q_PROPERTY(bool autoStartup READ autoStartup WRITE setAutoStartup NOTIFY sigAutoStartup)
public:
XTest(QObject* p = nullptr) {}
~XTest() {}
public:
enum Priority
{
High,
Low,
VeryHigh,
VeryLow
};
Q_ENUM(Priority)
enum PriorityFlag
{
High1,
Low1,
VeryHigh1,
VeryLow1
};
Q_FLAG(PriorityFlag)
Q_DECLARE_FLAGS(Alignment, PriorityFlag)
Q_FLAG(Alignment)
//Q_DECLARE_OPERATORS_FOR_FLAGS(Alignment)
public:
bool autoStartup() { return m_autoStartup; }
void setAutoStartup(bool b) { m_autoStartup = b; emit sigAutoStartup(); }
public slots:
void slot1(double* p1) {}
Q_INVOKABLE void slot2(int* p2) {}
Q_REVISION(1) void slot3(char* p3) {}
signals:
void sig1(void * p4);
void sigAutoStartup();
private:
bool m_autoStartup = false;
};
函数定义和声明都写在头文件,为了qt库机制写一个空的test.cpp
test.cpp
#include "XTest.h"
qt生成的moc_test.cpp文件:
/****************************************************************************
** Meta object code from reading C++ file 'XTest.h'
**
** Created by: The Qt Meta Object Compiler version 67 (Qt 5.15.2)
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/
#include <memory>
#include "../../../XTest.h"
#include <QtCore/qbytearray.h>
#include <QtCore/qmetatype.h>
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'XTest.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 67
#error "This file was generated using the moc from 5.15.2. It"
#error "cannot be used with the include files from this version of Qt."
#error "(The moc has changed too much.)"
#endif
QT_BEGIN_MOC_NAMESPACE
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
struct qt_meta_stringdata_XTest_t {
QByteArrayData data[28];
char stringdata0[206];
};
#define QT_MOC_LITERAL(idx, ofs, len) \
Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
qptrdiff(offsetof(qt_meta_stringdata_XTest_t, stringdata0) + ofs \
- idx * sizeof(QByteArrayData)) \
)
static const qt_meta_stringdata_XTest_t qt_meta_stringdata_XTest = {
{
QT_MOC_LITERAL(0, 0, 5), // "XTest"
QT_MOC_LITERAL(1, 6, 6), // "author"
QT_MOC_LITERAL(2, 13, 22), // "Sabrina Schweinsteiger"
QT_MOC_LITERAL(3, 36, 4), // "sig1"
QT_MOC_LITERAL(4, 41, 0), // ""
QT_MOC_LITERAL(5, 42, 2), // "p4"
QT_MOC_LITERAL(6, 45, 14), // "sigAutoStartup"
QT_MOC_LITERAL(7, 60, 5), // "slot1"
QT_MOC_LITERAL(8, 66, 7), // "double*"
QT_MOC_LITERAL(9, 74, 2), // "p1"
QT_MOC_LITERAL(10, 77, 5), // "slot2"
QT_MOC_LITERAL(11, 83, 4), // "int*"
QT_MOC_LITERAL(12, 88, 2), // "p2"
QT_MOC_LITERAL(13, 91, 5), // "slot3"
QT_MOC_LITERAL(14, 97, 5), // "char*"
QT_MOC_LITERAL(15, 103, 2), // "p3"
QT_MOC_LITERAL(16, 106, 11), // "autoStartup"
QT_MOC_LITERAL(17, 118, 8), // "Priority"
QT_MOC_LITERAL(18, 127, 4), // "High"
QT_MOC_LITERAL(19, 132, 3), // "Low"
QT_MOC_LITERAL(20, 136, 8), // "VeryHigh"
QT_MOC_LITERAL(21, 145, 7), // "VeryLow"
QT_MOC_LITERAL(22, 153, 12), // "PriorityFlag"
QT_MOC_LITERAL(23, 166, 5), // "High1"
QT_MOC_LITERAL(24, 172, 4), // "Low1"
QT_MOC_LITERAL(25, 177, 9), // "VeryHigh1"
QT_MOC_LITERAL(26, 187, 8), // "VeryLow1"
QT_MOC_LITERAL(27, 196, 9) // "Alignment"
},
"XTest\0author\0Sabrina Schweinsteiger\0"
"sig1\0\0p4\0sigAutoStartup\0slot1\0double*\0"
"p1\0slot2\0int*\0p2\0slot3\0char*\0p3\0"
"autoStartup\0Priority\0High\0Low\0VeryHigh\0"
"VeryLow\0PriorityFlag\0High1\0Low1\0"
"VeryHigh1\0VeryLow1\0Alignment"
};
#undef QT_MOC_LITERAL
static const uint qt_meta_data_XTest[] = {
// content:
8, // revision
0, // classname
1, 14, // classinfo
5, 16, // methods
1, 59, // properties
3, 63, // enums/sets
0, 0, // constructors
0, // flags
2, // signalCount
// classinfo: key, value
1, 2,
// signals: name, argc, parameters, tag, flags
3, 1, 46, 4, 0x06 /* Public */,
6, 0, 49, 4, 0x06 /* Public */,
// slots: name, argc, parameters, tag, flags
7, 1, 50, 4, 0x0a /* Public */,
10, 1, 53, 4, 0x0a /* Public */,
13, 1, 56, 4, 0x8a /* Public | MethodRevisioned */,
// signals: revision
0,
0,
// slots: revision
0,
0,
1,
// signals: parameters
QMetaType::Void, QMetaType::VoidStar, 5,
QMetaType::Void,
// slots: parameters
QMetaType::Void, 0x80000000 | 8, 9,
QMetaType::Void, 0x80000000 | 11, 12,
QMetaType::Void, 0x80000000 | 14, 15,
// properties: name, type, flags
16, QMetaType::Bool, 0x00495103,
// properties: notify_signal_id
1,
// enums: name, alias, flags, count, data
17, 17, 0x0, 4, 78,
22, 22, 0x1, 4, 86,
27, 22, 0x1, 4, 94,
// enum data: key, value
18, uint(XTest::High),
19, uint(XTest::Low),
20, uint(XTest::VeryHigh),
21, uint(XTest::VeryLow),
23, uint(XTest::High1),
24, uint(XTest::Low1),
25, uint(XTest::VeryHigh1),
26, uint(XTest::VeryLow1),
23, uint(XTest::High1),
24, uint(XTest::Low1),
25, uint(XTest::VeryHigh1),
26, uint(XTest::VeryLow1),
0 // eod
};
void XTest::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
if (_c == QMetaObject::InvokeMetaMethod) {
auto *_t = static_cast<XTest *>(_o);
Q_UNUSED(_t)
switch (_id) {
case 0: _t->sig1((*reinterpret_cast< void*(*)>(_a[1]))); break;
case 1: _t->sigAutoStartup(); break;
case 2: _t->slot1((*reinterpret_cast< double*(*)>(_a[1]))); break;
case 3: _t->slot2((*reinterpret_cast< int*(*)>(_a[1]))); break;
case 4: _t->slot3((*reinterpret_cast< char*(*)>(_a[1]))); break;
default: ;
}
} else if (_c == QMetaObject::IndexOfMethod) {
int *result = reinterpret_cast<int *>(_a[0]);
{
using _t = void (XTest::*)(void * );
if (*reinterpret_cast<_t *>(_a[1]) == static_cast<_t>(&XTest::sig1)) {
*result = 0;
return;
}
}
{
using _t = void (XTest::*)();
if (*reinterpret_cast<_t *>(_a[1]) == static_cast<_t>(&XTest::sigAutoStartup)) {
*result = 1;
return;
}
}
}
#ifndef QT_NO_PROPERTIES
else if (_c == QMetaObject::ReadProperty) {
auto *_t = static_cast<XTest *>(_o);
Q_UNUSED(_t)
void *_v = _a[0];
switch (_id) {
case 0: *reinterpret_cast< bool*>(_v) = _t->autoStartup(); break;
default: break;
}
} else if (_c == QMetaObject::WriteProperty) {
auto *_t = static_cast<XTest *>(_o);
Q_UNUSED(_t)
void *_v = _a[0];
switch (_id) {
case 0: _t->setAutoStartup(*reinterpret_cast< bool*>(_v)); break;
default: break;
}
} else if (_c == QMetaObject::ResetProperty) {
}
#endif // QT_NO_PROPERTIES
}
QT_INIT_METAOBJECT const QMetaObject XTest::staticMetaObject = { {
QMetaObject::SuperData::link<QObject::staticMetaObject>(),
qt_meta_stringdata_XTest.data,
qt_meta_data_XTest,
qt_static_metacall,
nullptr,
nullptr
} };
const QMetaObject *XTest::metaObject() const
{
return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;
}
void *XTest::qt_metacast(const char *_clname)
{
if (!_clname) return nullptr;
if (!strcmp(_clname, qt_meta_stringdata_XTest.stringdata0))
return static_cast<void*>(this);
return QObject::qt_metacast(_clname);
}
int XTest::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
_id = QObject::qt_metacall(_c, _id, _a);
if (_id < 0)
return _id;
if (_c == QMetaObject::InvokeMetaMethod) {
if (_id < 5)
qt_static_metacall(this, _c, _id, _a);
_id -= 5;
} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
if (_id < 5)
*reinterpret_cast<int*>(_a[0]) = -1;
_id -= 5;
}
#ifndef QT_NO_PROPERTIES
else if (_c == QMetaObject::ReadProperty || _c == QMetaObject::WriteProperty
|| _c == QMetaObject::ResetProperty || _c == QMetaObject::RegisterPropertyMetaType) {
qt_static_metacall(this, _c, _id, _a);
_id -= 1;
} else if (_c == QMetaObject::QueryPropertyDesignable) {
_id -= 1;
} else if (_c == QMetaObject::QueryPropertyScriptable) {
_id -= 1;
} else if (_c == QMetaObject::QueryPropertyStored) {
_id -= 1;
} else if (_c == QMetaObject::QueryPropertyEditable) {
_id -= 1;
} else if (_c == QMetaObject::QueryPropertyUser) {
_id -= 1;
}
#endif // QT_NO_PROPERTIES
return _id;
}
// SIGNAL 0
void XTest::sig1(void * _t1)
{
void *_a[] = { nullptr, const_cast<void*>(reinterpret_cast<const void*>(std::addressof(_t1))) };
QMetaObject::activate(this, &staticMetaObject, 0, _a);
}
// SIGNAL 1
void XTest::sigAutoStartup()
{
QMetaObject::activate(this, &staticMetaObject, 1, nullptr);
}
QT_WARNING_POP
QT_END_MOC_NAMESPACE
QMetaEnum::name
获取qt_meta_stringdata_XTest中对应位置的枚举类型名称
在test类中我们一个声明了3个枚举Priority、PriorityFlag、Alignment
上文对应的是红框部分
QMetaEnum::enumName
对于普通枚举作用和name一样,比如下图
PriorityFlag和Priority的name和enumName返回值都一样分别是字符串“PriorityFlag”和”Priority”。
但是对于Alignment则不同,name返回“Alignment”,enumName返回“PriorityFlag”。
QMetaEnum::keyCount
获取有多少个枚举值,Priority、PriorityFlag、Alignment对应的是:
QMetaEnum::isFlag
判断枚举值是否为标志
在moc对应的是
判断这个值是否为1,1就为标志
QMetaEnum::isScoped
判断是否为枚举类
在moc对应的是
判断这个值是否为2,2就为枚举类
QMetaEnum::key
获取枚举对应字符串
比如获取Alignment的High1字符串
QMetaEnum::value
获取枚举对应的值
比如获取Alignment::High1的值
QMetaEnum::scope
返回枚举所属类名称
QMetaEnum::keyToValue
获取字符串变量对应值
例如"VeryLow1"对应值是3
QMetaEnum::valueToKey
获取值对应枚举名称
QMetaEnum::keysToValue
字符串用“|”分割,获取枚举对应数值
QMetaEnum::valueToKeys
返回值对应的枚举名称
QMetaEnum::enclosingMetaObject
返回QMetaEnum所属的QMetaObject对象
QMetaEnum::isValid
内部调用QMetaEnum::name()函数,如果name为空则本函数返回false