简述
这里并不全面介绍Qt的信号槽机制的实现,仅以qt-opensource-windows-x86-msvc2015_64-5.6.0的源码为原料,以一个简单的信号槽例子为点,打通一条线。下面的源代码大部分是经过删减和修改的,为了使源码更简单并且增加可读性。
简单的信号槽例子
在vs2015中新建qt控制台项目,然后添加一个头文件和一个源文件。
mian.h内容
#pragma once
#include <QObject>
class Counter :public QObject
{
Q_OBJECT
public:
Counter() :m_value(0) {}
void count()
{
++m_value;
emit valueChanged(m_value);
}
public slots:
void printValue();
signals:
void valueChanged(int value);
void valueReset();
private:
int m_value;
};
main.cpp内容
#include <QCoreApplication>
#include <iostream>
#include "main.h"
void Counter::printValue()
{
std::cout << m_value << std::endl;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Counter c;
QObject::connect(&c, &Counter::valueChanged, &c, &Counter::printValue);
c.count();
return a.exec();
}
编译之后qt自动生成moc_main.cpp
#include "../../main.h"
#include <QtCore/qbytearray.h>
#include <QtCore/qmetatype.h>
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'main.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 67
#error "This file was generated using the moc from 5.6.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
struct qt_meta_stringdata_Counter_t {
QByteArrayData data[6];
char stringdata0[50];
};
#define QT_MOC_LITERAL(idx, ofs, len) \
Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
qptrdiff(offsetof(qt_meta_stringdata_Counter_t, stringdata0) + ofs \
- idx * sizeof(QByteArrayData)) \
)
static const qt_meta_stringdata_Counter_t qt_meta_stringdata_Counter = {
{
QT_MOC_LITERAL(0, 0, 7), // "Counter"
QT_MOC_LITERAL(1, 8, 12), // "valueChanged"
QT_MOC_LITERAL(2, 21, 0), // ""
QT_MOC_LITERAL(3, 22, 5), // "value"
QT_MOC_LITERAL(4, 28, 10), // "valueReset"
QT_MOC_LITERAL(5, 39, 10) // "printValue"
},
"Counter\0valueChanged\0\0value\0valueReset\0"
"printValue"
};
#undef QT_MOC_LITERAL
static const uint qt_meta_data_Counter[] = {
// content:
7, // revision
0, // classname
0, 0, // classinfo
3, 14, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
0, // flags
2, // signalCount
// signals: name, argc, parameters, tag, flags
1, 1, 29, 2, 0x06 /* Public */,
4, 0, 32, 2, 0x06 /* Public */,
// slots: name, argc, parameters, tag, flags
5, 0, 33, 2, 0x0a /* Public */,
// signals: parameters
QMetaType::Void, QMetaType::Int, 3,
QMetaType::Void,
// slots: parameters
QMetaType::Void,
0 // eod
};
void Counter::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
if (_c == QMetaObject::InvokeMetaMethod) {
Counter *_t = static_cast<Counter *>(_o);
Q_UNUSED(_t)
switch (_id) {
case 0: _t->valueChanged((*reinterpret_cast< int(*)>(_a[1]))); break;
case 1: _t->valueReset(); break;
case 2: _t->printValue(); break;
default: ;
}
} else if (_c == QMetaObject::IndexOfMethod) {
int *result = reinterpret_cast<int *>(_a[0]);
void **func = reinterpret_cast<void **>(_a[1]);
{
typedef void (Counter::*_t)(int );
if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&Counter::valueChanged)) {
*result = 0;
return;
}
}
{
typedef void (Counter::*_t)();
if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&Counter::valueReset)) {
*result = 1;
return;
}
}
}
}
const QMetaObject Counter::staticMetaObject = {
{ &QObject::staticMetaObject, qt_meta_stringdata_Counter.data,
qt_meta_data_Counter, qt_static_metacall, Q_NULLPTR, Q_NULLPTR}
};
const QMetaObject *Counter::metaObject() const
{
return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;
}
void *Counter::qt_metacast(const char *_clname)
{
if (!_clname) return Q_NULLPTR;
if (!strcmp(_clname, qt_meta_stringdata_Counter.stringdata0))
return static_cast<void*>(const_cast< Counter*>(this));
return QObject::qt_metacast(_clname);
}
int Counter::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 < 3)
qt_static_metacall(this, _c, _id, _a);
_id -= 3;
} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
if (_id < 3)
*reinterpret_cast<int*>(_a[0]) = -1;
_id -= 3;
}
return _id;
}
// SIGNAL 0
void Counter::valueChanged(int _t1)
{
void *_a[] = { Q_NULLPTR, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
QMetaObject::activate(this, &staticMetaObject, 0, _a);
}
// SIGNAL 1
void Counter::valueReset()
{
QMetaObject::activate(this, &staticMetaObject, 1, Q_NULLPTR);
}
QT_END_MOC_NAMESPACE
上面moc_main.cpp的代码有些地方不好理解,并且在本例中不需要用到,把这些内容删除,简化后如下
static const uint qt_meta_data_Counter[] = {
// content:
7, // revision
0, // classname
0, 0, // classinfo
3, 14, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
0, // flags
2, // signalCount
};
void Counter::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
if (_c == QMetaObject::InvokeMetaMethod)
{ // 根据_id调用信号或者槽函数
Counter *_t = static_cast<Counter *>(_o);
switch (_id) {
case 0: _t->valueChanged((*reinterpret_cast< int(*)>(_a[1]))); break;
case 1: _t->valueReset(); break;
case 2: _t->printValue(); break;
default:;
}
}
else if (_c == QMetaObject::IndexOfMethod)
{ // 根据信号函数指针获取其_id
int *result = reinterpret_cast<int *>(_a[0]);
void **func = reinterpret_cast<void **>(_a[1]);
{
typedef void (Counter::*_t)(int);
if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&Counter::valueChanged))
{
*result = 0;
return;
}
}
{
typedef void (Counter::*_t)();
if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&Counter::valueReset)) {
*result = 1;
return;
}
}
}
}
// 每个QObject或者QObject子类型都包含一个静态QMetaObject对象
// QMetaObject对象初始化
const QMetaObject Counter::staticMetaObject = {
{
// 父类元对象指针
&QObject::staticMetaObject, nullptr,
qt_meta_data_Counter,
qt_static_metacall/*d.static_metacall*/, nullptr, nullptr
}
};
const QMetaObject *Counter::metaObject() const
{
return &staticMetaObject;
}
// SIGNAL 0
void Counter::valueChanged(int _t1)
{
// _a[0]是返回值存放地址,这里返回类型为void,所以是nullptr
void *_a[] = { nullptr, &_t1 };
QMetaObject::activate(this, &staticMetaObject, 0, _a);
}
// SIGNAL 1
void Counter::valueReset()
{
// 既没有参数也没有返回值,所以最后一个参数是nullptr
QMetaObject::activate(this, &staticMetaObject, 1, nullptr);
}
QMetaObject介绍
在这不全面介绍QMetaObject,可以参考Qt手册,仅仅介绍本例相关的一些QMetaObject实现细节。
Qt手册上说QMetaObject类包含了Qt对象的元数据,例如类名、父类信息、方法属性等等,每个QObject类或子类都对应一个QMetaObject对象。在信号槽机制中,信号和槽的连接关系的保存,信号触发时执行槽函数这些操作也需要通过QMetaObject才能进行。
下面是Qt手册对元对象的介绍:
The meta-object system is based on three things:
1. The QObject class provides a base class for objects thatcan take advantage of the meta-object system.