为了便于通过调试进源码探究下Qt信号槽实现原理,这里简单写一段代码如下所示。
1.自定义信号槽连接
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow) {ui->setupUi(this);QObject::connect(ui->pushButton,&QPushButton::clicked,this,&MainWindow::on_pushButton_clicked);
}
MainWindow::~MainWindow() { delete ui;}
void MainWindow::on_pushButton_clicked() { qDebug() << "this is slot function"; }
2.调试进入源码观察槽函数调用流程
为了观察QPushButton::clicked()信号发射后如何自动调用on_pushButton_clicked槽函数,首先在源码里打上断点,通过如下流程最后实现由信号发射到槽函数调用。
// step1.这里打上断点
void QAbstractButton::mouseReleaseEvent(QMouseEvent *e)
{Q_D(QAbstractButton);if (e->button() != Qt::LeftButton) {e->ignore();return;}d->pressed = false;if (!d->down) {// refresh is required by QMacStyle to resume the default button animationd->refresh();e->ignore();return;}if (hitButton(e->pos())) {d->repeatTimer.stop();d->click(); // 这里发射click信号e->accept();} else {setDown(false);e->ignore();}
}
// step2. 进入click函数
void QAbstractButtonPrivate::click()
{Q_Q(QAbstractButton);down = false;blockRefresh = true;bool changeState = true;if (checked && queryCheckedButton() == q) {// the checked button of an exclusive or autoexclusive group cannot be unchecked
#if QT_CONFIG(buttongroup)if (group ? group->d_func()->exclusive : autoExclusive)
#elseif (autoExclusive)
#endifchangeState = false;}QPointer<QAbstractButton> guard(q);if (changeState) {q->nextCheckState();if (!guard)return;}blockRefresh = false;refresh();q->repaint();if (guard)emitReleased();if (guard)emitClicked();
}
// step3. 发射clciked
void QAbstractButtonPrivate::emitClicked()
{Q_Q(QAbstractButton);QPointer<QAbstractButton> guard(q);// 这里F11进入activate函数,该函数在moc_abstractbutton.cpp文件中,该文件不可见emit q->clicked(checked);
#if QT_CONFIG(buttongroup)if (guard && group) {emit group->buttonClicked(group->id(q));if (guard && group)emit group->buttonClicked(q);}
#endif
}
// step4. 元对象自动生成的activate函数
void QMetaObject::activate(QObject *sender, const QMetaObject *m, int local_signal_index,void **argv)
{activate(sender, QMetaObjectPrivate::signalOffset(m), local_signal_index, argv);
}
// step5 进入activate函数
void QMetaObject::activate(QObject *sender, int signalOffset, int local_signal_index, void **argv)
{int signal_index = signalOffset + local_signal_index;if (sender->d_func()->blockSig)return;Q_TRACE_SCOPE(QMetaObject_activate, sender, signal_index);if (sender->d_func()->isDeclarativeSignalConnected(signal_index)&& QAbstractDeclarativeData::signalEmitted) {Q_TRACE_SCOPE(QMetaObject_activate_declarative_signal, sender, signal_index);QAbstractDeclarativeData::signalEmitted(sender->d_func()->declarativeData, sender,signal_index, argv);}if (!sender->d_func()->isSignalConnected(signal_index, /*checkDeclarative =*/ false)&& !qt_signal_spy_callback_set.signal_begin_callback&& !qt_signal_spy_callback_set.signal_end_callback) {// The possible declarative connection is done, and nothing else is connected, so:return;}void *empty_argv[] = { 0 };if (qt_signal_spy_callback_set.signal_begin_callback != 0) {qt_signal_spy_callback_set.signal_begin_callback(sender, signal_index,argv ? argv : empty_argv);}{QMutexLocker locker(signalSlotLock(sender));struct ConnectionListsRef {QObjectConnectionListVector *connectionLists;ConnectionListsRef(QObjectConnectionListVector *connectionLists) : connectionLists(connectionLists){if (connectionLists)++connectionLists->inUse;}~ConnectionListsRef(){if (!connectionLists)return;--connectionLists->inUse;Q_ASSERT(connectionLists->inUse >= 0);if (connectionLists->orphaned) {if (!connectionLists->inUse)delete connectionLists;}}QObjectConnectionListVector *operator->() const { return connectionLists; }};// 1.获取sender里所有connection列表ConnectionListsRef connectionLists = sender->d_func()->connectionLists;if (!connectionLists.connectionLists) {locker.unlock();if (qt_signal_spy_callback_set.signal_end_callback != 0)qt_signal_spy_callback_set.signal_end_callback(sender, signal_index);return;}const QObjectPrivate::ConnectionList *list;if (signal_index < connectionLists->count())list = &connectionLists->at(signal_index);elselist = &connectionLists->allsignals;Qt::HANDLE currentThreadId = QThread::currentThreadId();// 2.在sender的connection列表中遍历do {QObjectPrivate::Connection *c = list->first;if (!c) continue;// We need to check against last here to ensure that signals added// during the signal emission are not emitted in this emission.QObjectPrivate::Connection *last = list->last;do {if (!c->receiver)continue;QObject * const receiver = c->receiver;const bool receiverInSameThread = currentThreadId == receiver->d_func()->threadData->threadId.load();// determine if this connection should be sent immediately or// put into the event queueif ((c->connectionType == Qt::AutoConnection && !receiverInSameThread)|| (c->connectionType == Qt::QueuedConnection)) {queued_activate(sender, signal_index, c, argv ? argv : empty_argv, locker);continue;
#if QT_CONFIG(thread)} else if (c->connectionType == Qt::BlockingQueuedConnection) {if (receiverInSameThread) {qWarning("Qt: Dead lock detected while activating a BlockingQueuedConnection: ""Sender is %s(%p), receiver is %s(%p)",sender->metaObject()->className(), sender,receiver->metaObject()->className(), receiver);}QSemaphore semaphore;QMetaCallEvent *ev = c->isSlotObject ?new QMetaCallEvent(c->slotObj, sender, signal_index, 0, 0, argv ? argv : empty_argv, &semaphore) :new QMetaCallEvent(c->method_offset, c->method_relative, c->callFunction, sender, signal_index, 0, 0, argv ? argv : empty_argv, &semaphore);QCoreApplication::postEvent(receiver, ev);locker.unlock();semaphore.acquire();locker.relock();continue;
#endif}QConnectionSenderSwitcher sw;if (receiverInSameThread) {sw.switchSender(receiver, sender, signal_index);}if (c->isSlotObject) {c->slotObj->ref();QScopedPointer<QtPrivate::QSlotObjectBase, QSlotObjectBaseDeleter> obj(c->slotObj);locker.unlock();{Q_TRACE_SCOPE(QMetaObject_activate_slot_functor, obj.data());obj->call(receiver, argv ? argv : empty_argv);}// Make sure the slot object gets destroyed before the mutex is locked again, as the// destructor of the slot object might also lock a mutex from the signalSlotLock() mutex pool,// and that would deadlock if the pool happens to return the same mutex.obj.reset();locker.relock();} else if (c->callFunction && c->method_offset <= receiver->metaObject()->methodOffset()) {//we compare the vtable to make sure we are not in the destructor of the object.const int methodIndex = c->method();const int method_relative = c->method_relative;const auto callFunction = c->callFunction;locker.unlock();if (qt_signal_spy_callback_set.slot_begin_callback != 0)qt_signal_spy_callback_set.slot_begin_callback(receiver, methodIndex, argv ? argv : empty_argv);{Q_TRACE_SCOPE(QMetaObject_activate_slot, receiver, methodIndex);callFunction(receiver, QMetaObject::InvokeMetaMethod, method_relative, argv ? argv : empty_argv);}if (qt_signal_spy_callback_set.slot_end_callback != 0)qt_signal_spy_callback_set.slot_end_callback(receiver, methodIndex);locker.relock();} else {const int method = c->method_relative + c->method_offset;locker.unlock();if (qt_signal_spy_callback_set.slot_begin_callback != 0) {qt_signal_spy_callback_set.slot_begin_callback(receiver,method,argv ? argv : empty_argv);}{Q_TRACE_SCOPE(QMetaObject_activate_slot, receiver, method);// 这里元调用metacall(receiver, QMetaObject::InvokeMetaMethod, method, argv ? argv : empty_argv); }if (qt_signal_spy_callback_set.slot_end_callback != 0)qt_signal_spy_callback_set.slot_end_callback(receiver, method);locker.relock();}if (connectionLists->orphaned)break;} while (c != last && (c = c->nextConnectionList) != 0);if (connectionLists->orphaned)break;} while (list != &connectionLists->allsignals &&//start over for all signals;((list = &connectionLists->allsignals), true));}if (qt_signal_spy_callback_set.signal_end_callback != 0)qt_signal_spy_callback_set.signal_end_callback(sender, signal_index);
}
// step6. 进入metalcall
int QMetaObject::metacall(QObject *object, Call cl, int idx, void **argv)
{if (object->d_ptr->metaObject)return object->d_ptr->metaObject->metaCall(object, cl, idx, argv);elsereturn object->qt_metacall(cl, idx, argv);
}
// step7. Q_OBJECT扩展,MOC.exe自动生成
int MainWindow::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{_id = QMainWindow::qt_metacall(_c, _id, _a);if (_id < 0)return _id;if (_c == QMetaObject::InvokeMetaMethod) {if (_id < 1)// 这里调用静态元调用qt_static_metacall(this, _c, _id, _a); _id -= 1;} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {if (_id < 1)*reinterpret_cast<int*>(_a[0]) = -1;_id -= 1;}return _id;
}
// step8 Q_OBJECT扩展,MOC.exe自动生成
void MainWindow::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{if (_c == QMetaObject::InvokeMetaMethod) {auto *_t = static_cast<MainWindow *>(_o);Q_UNUSED(_t)switch (_id) {// 这里最终调用槽函数case 0: _t->on_pushButton_clicked(); break; default: ;}}Q_UNUSED(_a);
}
从代码里可以看出Qt源码中大量使用d指针,从上面可以看出Qt元对象系统内维护了sender的连接链表,接下来看下connect函数是如何将信号和槽函数加入链表中去的。
3.观察connect函数实现
打开QObject::connect函数,进入源码。
// step 1 进入connect源码中实现template <typename Func1, typename Func2>static inline typename std::enable_if<QtPrivate::FunctionPointer<Func2>::ArgumentCount == -1, QMetaObject::Connection>::typeconnect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, const QObject *context, Func2 slot,Qt::ConnectionType type = Qt::AutoConnection){typedef QtPrivate::FunctionPointer<Func1> SignalType;const int FunctorArgumentCount = QtPrivate::ComputeFunctorArgumentCount<Func2 , typename SignalType::Arguments>::Value;Q_STATIC_ASSERT_X((FunctorArgumentCount >= 0),"Signal and slot arguments are not compatible.");const int SlotArgumentCount = (FunctorArgumentCount >= 0) ? FunctorArgumentCount : 0;typedef typename QtPrivate::FunctorReturnType<Func2, typename QtPrivate::List_Left<typename SignalType::Arguments, SlotArgumentCount>::Value>::Value SlotReturnType;Q_STATIC_ASSERT_X((QtPrivate::AreArgumentsCompatible<SlotReturnType, typename SignalType::ReturnType>::value),"Return type of the slot is not compatible with the return type of the signal.");Q_STATIC_ASSERT_X(QtPrivate::HasQ_OBJECT_Macro<typename SignalType::Object>::Value,"No Q_OBJECT in the class with the signal");const int *types = nullptr;if (type == Qt::QueuedConnection || type == Qt::BlockingQueuedConnection)types = QtPrivate::ConnectionTypes<typename SignalType::Arguments>::types();return connectImpl(sender, reinterpret_cast<void **>(&signal), context, nullptr,new QtPrivate::QFunctorSlotObject<Func2, SlotArgumentCount,typename QtPrivate::List_Left<typename SignalType::Arguments, SlotArgumentCount>::Value,typename SignalType::ReturnType>(std::move(slot)),type, types, &SignalType::Object::staticMetaObject);}
// step2
QMetaObject::Connection QObject::connectImpl(const QObject *sender, void **signal,const QObject *receiver, void **slot,QtPrivate::QSlotObjectBase *slotObj, Qt::ConnectionType type,const int *types, const QMetaObject *senderMetaObject)
{if (!signal) {qWarning("QObject::connect: invalid null parameter");if (slotObj)slotObj->destroyIfLastRef();return QMetaObject::Connection();}int signal_index = -1;void *args[] = { &signal_index, signal };for (; senderMetaObject && signal_index < 0; senderMetaObject = senderMetaObject->superClass()) {senderMetaObject->static_metacall(QMetaObject::IndexOfMethod, 0, args);if (signal_index >= 0 && signal_index < QMetaObjectPrivate::get(senderMetaObject)->signalCount)break;}if (!senderMetaObject) {qWarning("QObject::connect: signal not found in %s", sender->metaObject()->className());slotObj->destroyIfLastRef();return QMetaObject::Connection(0);}signal_index += QMetaObjectPrivate::signalOffset(senderMetaObject);return QObjectPrivate::connectImpl(sender, signal_index, receiver, slot, slotObj, type, types, senderMetaObject);
}
// step2. 进入connectImpl函数
QMetaObject::Connection QObjectPrivate::connectImpl(const QObject *sender, int signal_index,const QObject *receiver, void **slot,QtPrivate::QSlotObjectBase *slotObj, Qt::ConnectionType type,const int *types, const QMetaObject *senderMetaObject)
{if (!sender || !receiver || !slotObj || !senderMetaObject) {const char *senderString = sender ? sender->metaObject()->className(): senderMetaObject ? senderMetaObject->className(): "Unknown";const char *receiverString = receiver ? receiver->metaObject()->className(): "Unknown";qWarning("QObject::connect(%s, %s): invalid null parameter", senderString, receiverString);if (slotObj)slotObj->destroyIfLastRef();return QMetaObject::Connection();}QObject *s = const_cast<QObject *>(sender);QObject *r = const_cast<QObject *>(receiver);QOrderedMutexLocker locker(signalSlotLock(sender),signalSlotLock(receiver));if (type & Qt::UniqueConnection && slot) {QObjectConnectionListVector *connectionLists = QObjectPrivate::get(s)->connectionLists;if (connectionLists && connectionLists->count() > signal_index) {const QObjectPrivate::Connection *c2 =(*connectionLists)[signal_index].first;while (c2) {if (c2->receiver == receiver && c2->isSlotObject && c2->slotObj->compare(slot)) {slotObj->destroyIfLastRef();return QMetaObject::Connection();}c2 = c2->nextConnectionList;}}type = static_cast<Qt::ConnectionType>(type ^ Qt::UniqueConnection);}QScopedPointer<QObjectPrivate::Connection> c(new QObjectPrivate::Connection);c->sender = s;c->signal_index = signal_index;c->receiver = r;c->slotObj = slotObj;c->connectionType = type;c->isSlotObject = true;if (types) {c->argumentTypes.store(types);c->ownArgumentTypes = false;}// 这里获取sender的d指针并调用addConnect函数,将信号索引和connection数据加入链表中QObjectPrivate::get(s)->addConnection(signal_index, c.data());QMetaObject::Connection ret(c.take());locker.unlock();QMetaMethod method = QMetaObjectPrivate::signal(senderMetaObject, signal_index);Q_ASSERT(method.isValid());s->connectNotify(method);return ret;
}
// step3 维护链表实现
void QObjectPrivate::addConnection(int signal, Connection *c)
{Q_ASSERT(c->sender == q_ptr);if (!connectionLists)connectionLists = new QObjectConnectionListVector();if (signal >= connectionLists->count())connectionLists->resize(signal + 1);ConnectionList &connectionList = (*connectionLists)[signal];if (connectionList.last) {connectionList.last->nextConnectionList = c;} else {connectionList.first = c;}connectionList.last = c;cleanConnectionLists();c->prev = &(QObjectPrivate::get(c->receiver)->senders);c->next = *c->prev;*c->prev = c;if (c->next)c->next->prev = &c->next;if (signal < 0) {connectedSignals[0].store(~0);connectedSignals[1].store(~0);} else if (signal < (int)sizeof(connectedSignals) * 8) {connectedSignals[signal >> 5].store(connectedSignals[signal >> 5].load() | (1 << (signal & 0x1f)));}
}
4.总结
上述两段代码摘自Qt5.12.12的源码,粗略查看下信号槽实现机制,对于很多细节完全没有关注,后续有时间应该要更加细致的拜读下。
5.参考资料
d指针、q指针以及Qt元对象系统相关资料可自行bing查询