Qt Core学习日记——第一天Moc文件

深度

本文主要分析Windows平台,Mac、Linux暂不涉及

本文只分析到Win32 API/Windows Com组件/STL库函数层次,再下层代码不做探究

本文QT版本5.15.2

Moc

头文件XTest.h:

#pragma once

#include

class XTest : public QObject

{

Q_OBJECT

Q_CLASSINFO("author", "Sabrina Schweinsteiger")

public:

XTest(QObject* p = nullptr);

~XTest();

public slots:

void slot1(double * p1);

Q_INVOKABLE void slot2(int * p2);

Q_REVISION(1) void slot3(char * p3);

signals:

void sig1(void * p4);

};

cpp文件XTest.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

#include "../../../XTest.h"

#include

#include

#if !defined(Q_MOC_OUTPUT_REVISION)

#error "The header file 'XTest.h' doesn't include ."

#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[15];

char stringdata0[91];

};

#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, 5), // "slot1"

QT_MOC_LITERAL(7, 51, 7), // "double*"

QT_MOC_LITERAL(8, 59, 2), // "p1"

QT_MOC_LITERAL(9, 62, 5), // "slot2"

QT_MOC_LITERAL(10, 68, 4), // "int*"

QT_MOC_LITERAL(11, 73, 2), // "p2"

QT_MOC_LITERAL(12, 76, 5), // "slot3"

QT_MOC_LITERAL(13, 82, 5), // "char*"

QT_MOC_LITERAL(14, 88, 2) // "p3"

},

"XTest\0author\0Sabrina Schweinsteiger\0"

"sig1\0\0p4\0slot1\0double*\0p1\0slot2\0int*\0"

"p2\0slot3\0char*\0p3"

};

#undef QT_MOC_LITERAL

static const uint qt_meta_data_XTest[] = {

// content:

8, // revision

0, // classname

1, 14, // classinfo

4, 16, // methods

0, 0, // properties

0, 0, // enums/sets

0, 0, // constructors

0, // flags

1, // signalCount

// classinfo: key, value

1, 2,

// signals: name, argc, parameters, tag, flags

3, 1, 40, 4, 0x06 /* Public */,

// slots: name, argc, parameters, tag, flags

6, 1, 43, 4, 0x0a /* Public */,

9, 1, 46, 4, 0x0a /* Public */,

12, 1, 49, 4, 0x8a /* Public | MethodRevisioned */,

// signals: revision

0,

// slots: revision

0,

0,

1,

// signals: parameters

QMetaType::Void, QMetaType::VoidStar, 5,

// slots: parameters

QMetaType::Void, 0x80000000 | 7, 8,

QMetaType::Void, 0x80000000 | 10, 11,

QMetaType::Void, 0x80000000 | 13, 14,

0 // eod

};

void XTest::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)

{

if (_c == QMetaObject::InvokeMetaMethod) {

auto *_t = static_cast(_o);

Q_UNUSED(_t)

switch (_id) {

case 0: _t->sig1((*reinterpret_cast< void*(*)>(_a[1]))); break;

case 1: _t->slot1((*reinterpret_cast< double*(*)>(_a[1]))); break;

case 2: _t->slot2((*reinterpret_cast< int*(*)>(_a[1]))); break;

case 3: _t->slot3((*reinterpret_cast< char*(*)>(_a[1]))); break;

default: ;

}

} else if (_c == QMetaObject::IndexOfMethod) {

int *result = reinterpret_cast(_a[0]);

{

using _t = void (XTest::*)(void * );

if (*reinterpret_cast<_t *>(_a[1]) == static_cast<_t>(&XTest::sig1)) {

*result = 0;

return;

}

}

}

}

QT_INIT_METAOBJECT const QMetaObject XTest::staticMetaObject = { {

QMetaObject::SuperData::link(),

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(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 < 4)

qt_static_metacall(this, _c, _id, _a);

_id -= 4;

} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {

if (_id < 4)

*reinterpret_cast(_a[0]) = -1;

_id -= 4;

}

return _id;

}

// SIGNAL 0

void XTest::sig1(void * _t1)

{

void *_a[] = { nullptr, const_cast(reinterpret_cast(std::addressof(_t1))) };

QMetaObject::activate(this, &staticMetaObject, 0, _a);

}

QT_WARNING_POP

QT_END_MOC_NAMESPACE

QT_BEGIN_MOC_NAMESPACE

如果没有定义QT_NAMESPACE或者定义了Q_MOC_RUN则为空

否则为using namespace ::QT_NAMESPACE

默认QT_BEGIN_MOC_NAMESPACE为空

QT_WARNING_PUSH

默认定义__pragma(warning(push)),存储每个警告的当前警告状态

QT_WARNING_DISABLE_DEPRECATED

__pragma(warning(disable: 4996))关闭编译警告4996

qt_meta_stringdata_XTest_t

定义数据结构用于存储元数据信息,包括类名称、函数名称

data、stringdata0并不是恒定大小,依照头文件定义函数、参数数量决定

QT_MOC_LITERAL

用于指定函数名称的位置

#define QT_MOC_LITERAL(idx, ofs, len) \

{ { {-1} }, len, 0, 0, qptrdiff(((::size_t)&reinterpret_cast((((qt_meta_stringdata_XTest_t*)0)->stringdata0))) + ofs - idx * sizeof(QByteArrayData))}

qt_meta_stringdata_XTest

存储当前类的元数据信息

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, 5), // "slot1"

QT_MOC_LITERAL(7, 51, 7), // "double*"

QT_MOC_LITERAL(8, 59, 2), // "p1"

QT_MOC_LITERAL(9, 62, 5), // "slot2"

QT_MOC_LITERAL(10, 68, 4), // "int*"

QT_MOC_LITERAL(11, 73, 2), // "p2"

QT_MOC_LITERAL(12, 76, 5), // "slot3"

QT_MOC_LITERAL(13, 82, 5), // "char*"

QT_MOC_LITERAL(14, 88, 2) // "p3"

},

"XTest\0author\0Sabrina Schweinsteiger\0"

"sig1\0\0p4\0slot1\0double*\0p1\0slot2\0int*\0"

"p2\0slot3\0char*\0p3"

};

将宏翻译后得

static const qt_meta_stringdata_XTest_t qt_meta_stringdata_XTest = {

{

QT_MOC_LITERAL(0, 0, 5), // "XTest"

{-1,5,0,0,0x90},//是对QByteArrayData的定义

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, 5), // "slot1"

QT_MOC_LITERAL(7, 51, 7), // "double*"

QT_MOC_LITERAL(8, 59, 2), // "p1"

QT_MOC_LITERAL(9, 62, 5), // "slot2"

QT_MOC_LITERAL(10, 68, 4), // "int*"

QT_MOC_LITERAL(11, 73, 2), // "p2"

QT_MOC_LITERAL(12, 76, 5), // "slot3"

QT_MOC_LITERAL(13, 82, 5), // "char*"

QT_MOC_LITERAL(14, 88, 2) // "p3"

},

"XTest\0author\0Sabrina Schweinsteiger\0"

//类名称后接classinfo定义‘\0’隔开

"sig1\0\0p4\0slot1\0double*\0p1\0slot2\0int*\0"

"p2\0slot3\0char*\0p3"

};

QT_MOC_LITERAL(0, 0, 5),定义的是"XTest\0sig1\0\0slot1\0slot2\0slot3"中"XTest"的位置,类型为QByteArray.替换宏后为{-1,5,0,0,0x90},-1是QByteArray的引用计数,5为QByteArrayData的大小,0为分配地址,0为预留容量,0x90为"XTest\0author\0Sabrina Schweinsteiger\0"中"XTest"的首位置

假若h文件中函数改为

void slot1(double * p1);

Q_INVOKABLE void slot2(int * p2);

Q_REVISION(1) void slot3(char * p3);

qt_meta_stringdata_XTest变化如下:

static const qt_meta_stringdata_XTest_t qt_meta_stringdata_XTest = {

{

QT_MOC_LITERAL(0, 0, 5), // "XTest"

QT_MOC_LITERAL(1, 6, 4), // "sig1"

QT_MOC_LITERAL(2, 11, 0), // ""

QT_MOC_LITERAL(3, 12, 5), // "slot1"

QT_MOC_LITERAL(4, 18, 7), // "double*"

QT_MOC_LITERAL(5, 26, 2), // "p1"

QT_MOC_LITERAL(6, 29, 5), // "slot2"

QT_MOC_LITERAL(7, 35, 4), // "int*"

QT_MOC_LITERAL(8, 40, 2), // "p2"

QT_MOC_LITERAL(9, 43, 5), // "slot3"

QT_MOC_LITERAL(10, 49, 5), // "char*"

QT_MOC_LITERAL(11, 55, 2) // "p3"

},

"XTest\0sig1\0\0slot1\0double*\0p1\0slot2\0"

"int*\0p2\0slot3\0char*\0p3"

};

每个函数QT_MOC_LITERAL(3, 12, 5), // "slot1"定义下面紧接QT_MOC_LITERAL(4, 18, 7), // "double*"参数类型,类型后面接参数名称QT_MOC_LITERAL(5, 26, 2), // "p1"

qt_meta_data_XTest

static const uint qt_meta_data_XTest[] = {

// content:

8, // revision

0, // classname

0, 0, // classinfo

4, 14, // methods

0, 0, // properties

0, 0, // enums/sets

0, 0, // constructors

0, // flags

1, // signalCount

// signals: name, argc, parameters, tag, flags

1, 0, 38, 2, 0x06 /* Public */,

// slots: name, argc, parameters, tag, flags

3, 1, 39, 2, 0x0a /* Public */,

6, 1, 42, 2, 0x0a /* Public */,

9, 1, 45, 2, 0x8a /* Public | MethodRevisioned */,

// signals: revision

0,

// slots: revision

0,

0,

1,

// signals: parameters

QMetaType::Void,

// slots: parameters

QMetaType::Void, 0x80000000 | 4, 5,

QMetaType::Void, 0x80000000 | 7, 8,

QMetaType::Void, 0x80000000 | 10, 11,

0 // eod

};

3, 1, 39, 2, 0x0a /* Public */,3代表QT_MOC_LITERAL(3, 12, 5), // "slot1"函数,1参数数量,39代表参数“p1”位置,0x0a代表函数属性

XTest::qt_static_metacall

主要对外函数调用,消息循环触发过来时通过此函数调用到槽函数

QMetaObject *XTest::metaObject

获取QObject元数据QMetaObject

写到最后

各位看官有什么意见或建议可以写到评论区,大家一起探讨

你可能感兴趣的:(QT,qt,windows,Windows)