看 ActiveQt 模块的源码,看到信号槽部分,实在看不懂了,只好回来继续学习 元对象系统 了。
在 Qt Meta Object system 学习(一) 中简单整理了Q_OBJECT宏 与 moc 生成的文件,在 Qt Meta Object system 学习(二) 中学习了属性系统的宏 Q_PROPERTY。现在该看看源码中信号槽相关的内容了(Qt4.7的源码,其他版本与此可能不同)
QObject::connect非常重要的函数,不是么?
函数一开始是这么一段:
{const void *cbdata[] = { sender, signal, receiver, method, &type };
if (QInternal::activateCallbacks(QInternal::ConnectCallback, (void **) cbdata))
return true;
}
- 这个复合语句的作用看不太懂,QInternal类的定义在 src/corelib/global/qnamespace.h 中,类成员的实现在 src/corelib/global/qglobal.cpp 中。
然后检测4个参数是否都非空
if (sender == 0 || receiver == 0 || signal == 0 || method == 0){
qWarning("QObject::connect: Cannot connect %s::%s to %s::%s",
sender ? sender->metaObject()->className() : "(null)",
(signal && *signal) ? signal+1 : "(null)",
receiver ? receiver->metaObject()->className() : "(null)",
(method && *method) ? method+1 : "(null)");
return false;
}
然后
if (!check_signal_macro(sender, signal, "connect", "bind"))return false;
const QMetaObject *smeta = sender->metaObject();
- 检测信号格式是否有效(是否是通过SIGNAL生成的),然后从元对象系统中获得信号的索引
与信号类似
if (!check_method_code(membcode, receiver, method, "connect"))return false;
const char *method_arg = method;
- 检测与信号连接的“信号或槽”是否符合格式,然后从元对象系统中获得相应的索引
检测信号与槽的参数是否匹配,对与Queued连接,检测参数是否注册到元对象系统
if (!QMetaObject::checkConnectArgs(signal, method)) {qWarning("QObject::connect: Incompatible sender/receiver arguments"
"\n %s::%s --> %s::%s",
sender->metaObject()->className(), signal,
receiver->metaObject()->className(), method);
return false;
}
int *types = 0;
if ((type == Qt::QueuedConnection || type == Qt::BlockingQueuedConnection)
&& !(types = queuedConnectionTypes(smeta->method(signal_absolute_index).parameterTypes())))
return false;
若这一切都通过了,调用 QMetaObjectPrivate 的connect成员。调用成功,则调用 sender 对象的 connectNofify 函数(ActiveQt模块使用了该函数)
if (!QMetaObjectPrivate::connect(sender, signal_index, receiver, method_index, type, types))return false;
const_cast<QObject*>(sender)->connectNotify(signal - 1);QMetaObjectPrivate::connect
首先,检测是否是 uniqueconnection ,如果是,则先搜索已有的连接,找到则函数返回
if (type & Qt::UniqueConnection) {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->method == method_index)
return false;
c2 = c2->nextConnectionList;
}
}
type &= Qt::UniqueConnection - 1;
}
创建连接
QObjectPrivate::Connection *c = new QObjectPrivate::Connection;c->sender = s;
c->receiver = r;
c->method = method_index;
c->connectionType = type;
c->argumentTypes = types;
c->nextConnectionList = 0;
QT_TRY {
QObjectPrivate::get(s)->addConnection(signal_index, c);
} QT_CATCH(...) {
delete c;
QT_RETHROW;
}
其他还有部分代码,看不太懂,暂略(应该不影响理解)
QMetaObject::activate信号的定义体在 moc 生成的文件内,在定义体中会调用 QMetaObject::activate 函数来实现信号的功能。
首先判断该信号有无被链接,是否是block设置(具体含义?)
if (!sender->d_func()->isSignalConnected(signal_index))return; // nothing connected to these signals, and no spy
if (sender->d_func()->blockSig)
return;
和signal spy有关的一些代码
void *empty_argv[] = { 0 };if (qt_signal_spy_callback_set.signal_begin_callback != 0) {
qt_signal_spy_callback_set.signal_begin_callback(sender, signal_absolute_index,
argv ? argv : empty_argv);
}
QThreadData *currentThreadData = QThreadData::current();
QMutexLocker locker(signalSlotLock(sender));
QObjectConnectionListVector *connectionLists = sender->d_func()->connectionLists; if (!connectionLists) {
locker.unlock();
if (qt_signal_spy_callback_set.signal_end_callback != 0)
qt_signal_spy_callback_set.signal_end_callback(sender, signal_absolute_index);
return;
}
++connectionLists->inUse;
用双重循环依次处理每一个信号的每一个连接
do {QObjectPrivate::Connection *c = connectionLists->at(signal_index).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 = connectionLists->at(signal_index).last;
do {
if (!c->receiver)
continue;
QObject * const receiver = c->receiver;
对queued的连接单独处理
if ((c->connectionType == Qt::AutoConnection&& (currentThreadData != sender->d_func()->threadData
|| receiver->d_func()->threadData != sender->d_func()->threadData))
|| (c->connectionType == Qt::QueuedConnection)) {
queued_activate(sender, signal_absolute_index, c, argv ? argv : empty_argv);
continue;
} else if (c->connectionType == Qt::BlockingQueuedConnection)
{
blocking_activate(sender, signal_absolute_index, c, argv ? argv : empty_argv);
continue;
}
对其他连接,继续(发送与接收者在同一线程,则设置当前发送者,QObject::sender() 需要该功能)
const int method = c->method;QObjectPrivate::Sender currentSender;
const bool receiverInSameThread = currentThreadData == receiver->d_func()->threadData;
QObjectPrivate::Sender *previousSender = 0;
if (receiverInSameThread) {
currentSender.sender = sender;
currentSender.signal = signal_absolute_index;
currentSender.ref = 1;
previousSender = QObjectPrivate::setCurrentSender(receiver, ¤tSender);
}
locker.unlock();
调用metacall
metacall(receiver, QMetaObject::InvokeMetaMethod, method, argv ? argv : empty_argv);QMetaObject::metacall int QMetaObject::metacall(QObject *object, Call cl, int idx, void **argv){
if (QMetaObject *mo = object->d_ptr->metaObject)
return static_cast<QAbstractDynamicMetaObject*>(mo)->metaCall(cl, idx, argv);
else
return object->qt_metacall(cl, idx, argv);
}
前面的 QAbstractDynamicMetaObject 部分不清楚作用,后面的 qt_metacall 比较好理解了(代码在moc生成的文件中)
queued_activate首先是参数检测
...if (c->argumentTypes == &DIRECT_CONNECTION_ONLY) // cannot activate
return;
然后是构建 QMetaCallEvent 事件,并post该事件
QCoreApplication::postEvent(c->receiver, new QMetaCallEvent(c->method,sender,
signal,
nargs,
types,
args,
semaphore));blocking_activate
检测是否是同一线程
if (QThread::currentThread() == c->receiver->thread()) {qWarning("Qt: Dead lock detected while activating a BlockingQueuedConnection: "
"Sender is %s(%p), receiver is %s(%p)",
sender->metaObject()->className(), sender,
c->receiver->metaObject()->className(), c->receiver);
}
调用
#ifdef QT_NO_THREADqueued_activate(sender, signal, c, argv);
#else
QSemaphore semaphore;
queued_activate(sender, signal, c, argv, &semaphore);
QMutex *mutex = signalSlotLock(sender);
mutex->unlock();
semaphore.acquire();
mutex->lock();
#endifQObject::event
既然与事件有关postEvent,我们就需要看看事件接收者的如何处理这个调用事件的
case QEvent::MetaCall:{
d_func()->inEventHandler = false;
QMetaCallEvent *mce = static_cast<QMetaCallEvent*>(e);
QObjectPrivate::Sender currentSender;
currentSender.sender = const_cast<QObject*>(mce->sender());
currentSender.signal = mce->signalId();
currentSender.ref = 1;
QObjectPrivate::Sender * const previousSender =
QObjectPrivate::setCurrentSender(this, ¤tSender);
#if defined(QT_NO_EXCEPTIONS)
mce->placeMetaCall(this);
#else
QT_TRY {
mce->placeMetaCall(this);
} QT_CATCH(...) {
QObjectPrivate::resetCurrentSender(this, ¤tSender, previousSender);
QT_RETHROW;
}
#endif
QObjectPrivate::resetCurrentSender(this, ¤tSender, previousSender);
break;
}
可以看到,这儿调用了 placeMetaCall
int QMetaCallEvent::placeMetaCall(QObject *object){
return QMetaObject::metacall(object, QMetaObject::InvokeMetaMethod, id_, args_);
}参考
-
http://blog.csdn.net/tingsking18/archive/2009/11/12/4800828.aspx
-
http://blog.csdn.net/tingsking18/archive/2009/12/12/4991563.aspx