我想知道Qt如何实现元对象编译,使他们能够在Qt中提供信号槽机制。我无法在文档中找到太多详细信息。
编辑::我无法获取
_id = QObject::qt_metacall(_c, _id, _a);
的源代码
以下是源文件和对应的moc文件。
#ifndef SSOBJECT_H
#define SSOBJECT_H
#include <QObject>
class ssObject : public QObject
{
Q_OBJECT
public:
explicit ssObject(QObject *parent = 0);
signals:
void readyToPrint();
void readyToPrint1(int);
void readyToPrint2(float);
private slots:
int print();
};
#endif // SSOBJECT_H
//output from MOC
#include "../ssobject.h"
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'ssobject.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 62
#error "This file was generated using the moc from 4.6.1. 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
static const uint qt_meta_data_ssObject[] = {
// content:
4, // revision
0, // classname
0, 0, // classinfo
4, 14, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
0, // flags
3, // signalCount
// signals: signature, parameters, type, tag, flags
10, 9, 9, 9, 0x05,
25, 9, 9, 9, 0x05,
44, 9, 9, 9, 0x05,
// slots: signature, parameters, type, tag, flags
69, 9, 65, 9, 0x08,
0 // eod
};
static const char qt_meta_stringdata_ssObject[] = {
"ssObject\0\0readyToPrint()\0readyToPrint1(int)\0"
"readyToPrint2(float)\0int\0print()\0"
};
const QMetaObject ssObject::staticMetaObject = {
{ &QObject::staticMetaObject, qt_meta_stringdata_ssObject,
qt_meta_data_ssObject, 0 }
};
#ifdef Q_NO_DATA_RELOCATION
const QMetaObject &ssObject::getStaticMetaObject() { return staticMetaObject; }
#endif //Q_NO_DATA_RELOCATION
const QMetaObject *ssObject::metaObject() const
{
return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject;
}
void *ssObject::qt_metacast(const char *_clname)
{
if (!_clname) return 0;
if (!strcmp(_clname, qt_meta_stringdata_ssObject))
return static_cast<void*>(const_cast< ssObject*>(this));
return QObject::qt_metacast(_clname);
}
int ssObject::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) {
switch (_id) {
case 0: readyToPrint(); break;
case 1: readyToPrint1((*reinterpret_cast< int(*)>(_a[1]))); break;
case 2: readyToPrint2((*reinterpret_cast< float(*)>(_a[1]))); break;
case 3: { int _r = print();
if (_a[0]) *reinterpret_cast< int*>(_a[0]) = _r; } break;
default: ;
}
_id -= 4;
}
return _id;
}
// SIGNAL 0
void ssObject::readyToPrint()
{
QMetaObject::activate(this, &staticMetaObject, 0, 0);
}
// SIGNAL 1
void ssObject::readyToPrint1(int _t1)
{
void *_a[] = { 0, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
QMetaObject::activate(this, &staticMetaObject, 1, _a);
}
// SIGNAL 2
void ssObject::readyToPrint2(float _t1)
{
void *_a[] = { 0, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
QMetaObject::activate(this, &staticMetaObject, 2, _a);
}
QT_END_MOC_NAMESPACE
还请解释如何 QObject::connect(ssobj1,SIGNAL(readyToPrint()),ssobj1,SLOT(print())) 扩大
或者更好的是,创建一个像这样的虚拟
QObject.h
文件
class QObject
{
Q_OBJECT
};
然后
moc QObject.h
,这就是我得到的:
/****************************************************************************
** Meta object code from reading C++ file 'QObject.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 ".\template\QObject.h"
#include <QtCore/qbytearray.h>
#include <QtCore/qmetatype.h>
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'QObject.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_QObject_t {
QByteArrayData data[1];
char stringdata0[8];
};
#define QT_MOC_LITERAL(idx, ofs, len) \
Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
qptrdiff(offsetof(qt_meta_stringdata_QObject_t, stringdata0) + ofs \
- idx * sizeof(QByteArrayData)) \
)
static const qt_meta_stringdata_QObject_t qt_meta_stringdata_QObject = {
{
QT_MOC_LITERAL(0, 0, 7) // "QObject"
},
"QObject"
};
#undef QT_MOC_LITERAL
static const uint qt_meta_data_QObject[] = {
// content:
8, // revision
0, // classname
0, 0, // classinfo
0, 0, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
0, // flags
0, // signalCount
0 // eod
};
void QObject::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
Q_UNUSED(_o);
Q_UNUSED(_id);
Q_UNUSED(_c);
Q_UNUSED(_a);
}
QT_INIT_METAOBJECT const QMetaObject QObject::staticMetaObject = { {
nullptr,
qt_meta_stringdata_QObject.data,
qt_meta_data_QObject,
qt_static_metacall,
nullptr,
nullptr
} };
const QMetaObject *QObject::metaObject() const
{
return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;
}
void *QObject::qt_metacast(const char *_clname)
{
if (!_clname) return nullptr;
if (!strcmp(_clname, qt_meta_stringdata_QObject.stringdata0))
return static_cast<void*>(this);
return nullptr;
}
int QObject::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
return _id;
}
QT_WARNING_POP
QT_END_MOC_NAMESPACE
从下面的代码来看,
QObject
的qt_metacall
也是由moc
生成的。
class Q_CORE_EXPORT QObject
{
Q_OBJECT
}
从下面的代码(https://github.com/qt/qtbase/blob/dev/src/tools/moc/generator.cpp#LL971C41-L971C50):
if (!purestSuperClass.isEmpty() && !isQObject) {
QByteArray superClass = purestSuperClass;
fprintf(out, " _id = %s::qt_metacall(_c, _id, _a);\n", superClass.constData());
}
ssObject::qt_metacall
和QObject::qt_metacall
的区别在于基点qt_metacall()
的调用。
所以我猜
QObject::qt_metacall
会类似于 ssObject::qt_metacall
但不会调用 _id = QObject::qt_metacall(_c, _id, _a);
。