Qt Meta-Object System
一、测试源码
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QPainter> #include <QSize> #include <QDebug> class Widget : public QWidget { Q_OBJECT Q_PROPERTY(int age READ age WRITE setAge NOTIFY ageChanged) public: Widget(QWidget *parent = 0); ~Widget(); void paintEvent(QPaintEvent* event); QSize sizeHint() const { return QSize(400, 400); } int age()const { return m_age; } void setAge(const int& age) { m_age = age; emit ageChanged(m_age); } protected: int m_age; signals: void sig0(); int sig1(int value); void ageChanged(int age); public slots: void st0() { qDebug() << "st0"; } private slots: void st1() { qDebug() << "st1"; } }; #endif // WIDGET_H
widget.cpp
#include "widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) { } Widget::~Widget() { } void Widget::paintEvent(QPaintEvent* event) { QPainter painter(this); painter.drawText(50, 50, "This is widget"); }
main.cpp
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
二、moc编译后
widget.h 宏展开
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QPainter> #include <QSize> #include <QDebug> class Widget : public QWidget { //Q_OBJECT public: static const QMetaObject staticMetaObject; virtual const QMetaObject *metaObject() const; virtual void *qt_metacast(const char *); virtual int qt_metacall(QMetaObject::Call, int, void **); private: static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); struct QPrivateSignal {}; //Q_OBJECT end //Q_PROPERTY(int age READ age WRITE setAge NOTIFY ageChanged) public: Widget(QWidget *parent = 0); ~Widget(); void paintEvent(QPaintEvent* event); QSize sizeHint() const { return QSize(400, 400); } int age()const { return m_age; } void setAge(const int& age) { m_age = age; emit ageChanged(m_age); } protected: int m_age; signals: void sig0(); int sig1(int value); void ageChanged(int age); public slots: void st0() { qDebug() << "st0"; } private slots: void st1() { qDebug() << "st1"; } }; #endif // WIDGET_H
moc_widget.cpp
#include "../../sample1/widget.h" #include <QtCore/qbytearray.h> #include <QtCore/qmetatype.h> #if !defined(Q_MOC_OUTPUT_REVISION) #error "The header file 'widget.h' doesn't include <QObject>." #elif Q_MOC_OUTPUT_REVISION != 67 #error "This file was generated using the moc from 5.12.0. 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_Widget_t { QByteArrayData data[9]; char stringdata0[47]; }; #define QT_MOC_LITERAL(idx, ofs, len) \ Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \ qptrdiff(offsetof(qt_meta_stringdata_Widget_t, stringdata0) + ofs \ - idx * sizeof(QByteArrayData)) \ ) static const qt_meta_stringdata_Widget_t qt_meta_stringdata_Widget = { { QT_MOC_LITERAL(0, 0, 6), // "Widget" QT_MOC_LITERAL(1, 7, 4), // "sig0" QT_MOC_LITERAL(2, 12, 0), // "" QT_MOC_LITERAL(3, 13, 4), // "sig1" QT_MOC_LITERAL(4, 18, 5), // "value" QT_MOC_LITERAL(5, 24, 10), // "ageChanged" QT_MOC_LITERAL(6, 35, 3), // "age" QT_MOC_LITERAL(7, 39, 3), // "st0" QT_MOC_LITERAL(8, 43, 3) // "st1" }, "Widget\0sig0\0\0sig1\0value\0ageChanged\0" "age\0st0\0st1" }; #undef QT_MOC_LITERAL static const uint qt_meta_data_Widget[] = { // content: 8, // revision 0, // classname 0, 0, // classinfo 5, 14, // methods 1, 48, // properties 0, 0, // enums/sets 0, 0, // constructors 0, // flags 3, // signalCount // signals: name, argc, parameters, tag, flags 1, 0, 39, 2, 0x06 /* Public */, 3, 1, 40, 2, 0x06 /* Public */, 5, 1, 43, 2, 0x06 /* Public */, // slots: name, argc, parameters, tag, flags 7, 0, 46, 2, 0x0a /* Public */, 8, 0, 47, 2, 0x08 /* Private */, // signals: parameters QMetaType::Void, QMetaType::Int, QMetaType::Int, 4, QMetaType::Void, QMetaType::Int, 6, // slots: parameters QMetaType::Void, QMetaType::Void, // properties: name, type, flags 6, QMetaType::Int, 0x00495103, // properties: notify_signal_id 2, 0 // eod }; void Widget::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a) { if (_c == QMetaObject::InvokeMetaMethod) { Widget *_t = static_cast<Widget *>(_o); Q_UNUSED(_t) switch (_id) { case 0: _t->sig0(); break; case 1: { int _r = _t->sig1((*reinterpret_cast< int(*)>(_a[1]))); if (_a[0]) *reinterpret_cast< int*>(_a[0]) = std::move(_r); } break; case 2: _t->ageChanged((*reinterpret_cast< int(*)>(_a[1]))); break; case 3: _t->st0(); break; case 4: _t->st1(); break; default: ; } } else if (_c == QMetaObject::IndexOfMethod) { int *result = reinterpret_cast<int *>(_a[0]); { using _t = void (Widget::*)(); if (*reinterpret_cast<_t *>(_a[1]) == static_cast<_t>(&Widget::sig0)) { *result = 0; return; } } { using _t = int (Widget::*)(int ); if (*reinterpret_cast<_t *>(_a[1]) == static_cast<_t>(&Widget::sig1)) { *result = 1; return; } } { using _t = void (Widget::*)(int ); if (*reinterpret_cast<_t *>(_a[1]) == static_cast<_t>(&Widget::ageChanged)) { *result = 2; return; } } } #ifndef QT_NO_PROPERTIES else if (_c == QMetaObject::ReadProperty) { Widget *_t = static_cast<Widget *>(_o); Q_UNUSED(_t) void *_v = _a[0]; switch (_id) { case 0: *reinterpret_cast< int*>(_v) = _t->age(); break; default: break; } } else if (_c == QMetaObject::WriteProperty) { Widget *_t = static_cast<Widget *>(_o); Q_UNUSED(_t) void *_v = _a[0]; switch (_id) { case 0: _t->setAge(*reinterpret_cast< int*>(_v)); break; default: break; } } else if (_c == QMetaObject::ResetProperty) { } #endif // QT_NO_PROPERTIES } QT_INIT_METAOBJECT const QMetaObject Widget::staticMetaObject = { { &QWidget::staticMetaObject, qt_meta_stringdata_Widget.data, qt_meta_data_Widget, qt_static_metacall, nullptr, nullptr } }; const QMetaObject *Widget::metaObject() const { return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject; } void *Widget::qt_metacast(const char *_clname) { if (!_clname) return nullptr; if (!strcmp(_clname, qt_meta_stringdata_Widget.stringdata0)) return static_cast<void*>(this); return QWidget::qt_metacast(_clname); } int Widget::qt_metacall(QMetaObject::Call _c, int _id, void **_a) { _id = QWidget::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 Widget::sig0() { QMetaObject::activate(this, &staticMetaObject, 0, nullptr); } // SIGNAL 1 int Widget::sig1(int _t1) { int _t0{}; void *_a[] = { const_cast<void*>(reinterpret_cast<const void*>(&_t0)), const_cast<void*>(reinterpret_cast<const void*>(&_t1)) }; QMetaObject::activate(this, &staticMetaObject, 1, _a); return _t0; } // SIGNAL 2 void Widget::ageChanged(int _t1) { void *_a[] = { nullptr, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) }; QMetaObject::activate(this, &staticMetaObject, 2, _a); } QT_WARNING_POP QT_END_MOC_NAMESPACE