1、 简述:QMetaObject是用来保存Qt元对象的元信息,当继承QObject类并使用宏Q_OBJECT时,创建的类产生一个静态QMetaObject实例staticMetaObject,这个实例会保存类名、信号名称及索引、槽的名字及索引等等在对象操作时需要的基本信息,而这也是信号与槽的根本所在:
#define Q_OBJECT \
public: \
Q_OBJECT_CHECK \
static const QMetaObject staticMetaObject; \
virtual const QMetaObject *metaObject() const; \
virtual void *qt_metacast(const char *); \
QT_TR_FUNCTIONS \
virtual int qt_metacall(QMetaObject::Call, int, void **); \
private: \
Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
struct QPrivateSignal {};
2、signals、slots、SIGNAL、SLOT、emit
(1)、Qt基于C++,那么就必须遵循C++的语法规则,否则编译出错;但在信号与槽的类声明中,如此使用:
{
... ...
signals:
void sig(...);
private slots:
void on_slots(....);
... ...
}
通过以上例子看出, signals、slots好像不是C++里的关键字,但在Qt中又是合法的修饰符,到底是什么原因呢?查看Qt源代码:
ifndef QT_NO_SIGNALS_SLOTS_KEYWORDS
# define slots
# define signals public
# endif
现在看来,他们都是地道的宏定义,只是包装了C++控制修饰符:public、private、protected,但是细看会发现信号void sig(...)未实现,而我们使用的语句 emit sig(...);何解? 再看:
# ifndef QT_NO_EMIT
# define emit
# endif
完美的一个空宏,也造就了完美的普通函数调用,关于这个普通的函数没有具体实现的原因,得从moc(meta object compiler)说起,通过查看moc_BasePage.cpp如下:
// SIGNAL 0
void BasePage::readyRead(quint16 _t1, char * _t2, quint16 _t3)
{
void *_a[] = { 0, const_cast(reinterpret_cast(&_t1)),
const_cast(reinterpret_cast(&_t2)),
const_cast(reinterpret_cast(&_t3)) };
QMetaObject::activate(this, &staticMetaObject, 0, _a);
}
由此得知,“信号函数”不需要开发人员实现,moc会自动实现“信号函数”。
(2)、连接信号与槽时:connect(sender, SIGNAL(...), receiver(...), SLOT(...))
粗略一看,大体已经知道了SIGNAL、SLOT是宏定义,其源代码为:
# ifndef QT_NO_KEYWORDS
# define METHOD(a) "0"#a
# endif
# define SLOT(a) "1"#a
# define SIGNAL(a) "2"#a
#endif
what it means?主要是使用了C语言的字节拼接符"#"把信号名称转换成了以2开头的字符串,Qt发送信号时才能在元数据中查找到与之对应的信号。
#include "../../untitled/metadatatest.h"
#include
#include
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'metadatatest.h' doesn't include ."
#elif Q_MOC_OUTPUT_REVISION != 67
#error "This file was generated using the moc from 5.3.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
struct qt_meta_stringdata_MetaDataTest_t {
QByteArrayData data[6];
char stringdata[51];
};//存储信号、槽、类名称及相关索引、偏移的结构体
#define QT_MOC_LITERAL(idx, ofs, len) \
Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
qptrdiff(offsetof(qt_meta_stringdata_MetaDataTest_t, stringdata) + ofs \
- idx * sizeof(QByteArrayData)) \
)
static const qt_meta_stringdata_MetaDataTest_t qt_meta_stringdata_MetaDataTest = {
{
QT_MOC_LITERAL(0, 0, 12),
QT_MOC_LITERAL(1, 13, 8),
QT_MOC_LITERAL(2, 22, 0),
QT_MOC_LITERAL(3, 23, 7),
QT_MOC_LITERAL(4, 31, 8),
QT_MOC_LITERAL(5, 40, 10)
},
"MetaDataTest\0msgPrint\0\0sendMsg\0printMsg\0"
"receiveMsg"
}; //moc查看头文件,根据signals、slots提取信号与槽的名称与索引保存到qt_meta_stringdata_MetaDataTest 中
#undef QT_MOC_LITERAL
static const uint qt_meta_data_MetaDataTest[] = {
// content:
7, // revision
0, // classname
0, 0, // classinfo
2, 14, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
0, // flags
1, // signalCount
// signals: name, argc, parameters, tag, flags
1, 1, 24, 2, 0x06 /* Public */,
// slots: name, argc, parameters, tag, flags
4, 1, 27, 2, 0x08 /* Private */,
// signals: parameters
QMetaType::Void, QMetaType::QString, 3,
// slots: parameters
QMetaType::Void, QMetaType::QString, 5,
0 // eod
}; //建立类构造、方法、属性等的元数据,保存到类全局数组qt_meta_data_MetaDataTest中
void MetaDataTest::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
if (_c == QMetaObject::InvokeMetaMethod) {
MetaDataTest *_t = static_cast(_o);
switch (_id) {
case 0: _t->msgPrint((*reinterpret_cast< QString(*)>(_a[1]))); break;
case 1: _t->printMsg((*reinterpret_cast< QString(*)>(_a[1]))); break;
default: ;
}
} else if (_c == QMetaObject::IndexOfMethod) {
int *result = reinterpret_cast(_a[0]);
void **func = reinterpret_cast(_a[1]);
{
typedef void (MetaDataTest::*_t)(QString );
if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&MetaDataTest::msgPrint)) {
*result = 0;
}
}
}
} //Param1:MetaDataTest实例指针;Param2:操作类型(枚举);Param3:信号或槽索引;Param4:不知道干嘛
const QMetaObject MetaDataTest::staticMetaObject = {
{ &QObject::staticMetaObject, qt_meta_stringdata_MetaDataTest.data,
qt_meta_data_MetaDataTest, qt_static_metacall, 0, 0}
}; //关联qt_meta_stringdata_MetaDataTest、qt_meta_data_MetaDataTest元数据到staticMetaObject
const QMetaObject *MetaDataTest::metaObject() const
{
return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;
}//返回当前对象的元对象,可以根据此元对象获取索引、类名等信息,在基类QObject中调用
void *MetaDataTest::qt_metacast(const char *_clname)
{
if (!_clname) return 0;
if (!strcmp(_clname, qt_meta_stringdata_MetaDataTest.stringdata))
return static_cast(const_cast< MetaDataTest*>(this));
return QObject::qt_metacast(_clname);
}//类、信号、槽名称比较
int MetaDataTest::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 < 2)
qt_static_metacall(this, _c, _id, _a);
_id -= 2;
} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
if (_id < 2)
*reinterpret_cast(_a[0]) = -1;
_id -= 2;
}
return _id;
}//其中id就是信号或槽的索引
void MetaDataTest::msgPrint(QString _t1)
{
void *_a[] = { 0, const_cast(reinterpret_cast(&_t1)) };
QMetaObject::activate(this, &staticMetaObject, 0, _a);
} // SIGNAL 0 (信号索引)
QT_END_MOC_NAMESPACE
4、void QMetaObject::activate(QObject sender, int signalOffset, int local_signal_index);
(1)、void QMetaObject::activate(QObject *sender, const QMetaObject *m, int local_signal_index, void **argv){
//当前信号通过m查找到信号的索引和偏移量并触发相应的信号或槽动作
activate(sender, QMetaObjectPrivate::signalOffset(m), local_signal_index, argv);//很长的函数
}//据我理解信号的触发从这里开始