Qt中postEvent和sendEvent函数
部分内容参考http://blog.csdn.net/lvmengzou/article/details/65450908
qt事件循环需要维护一个事件队列,在Qt的main函数中最后一般调用QApplication::exec()成员函数来保持程序对事件队列的处理,exec()的实质是不停调用processEvent()函数从队列中获取事件,并处理,然后删除,postEvent的作用就是发送一个事件到此队列中,由于删除队列中事件调用delete运算符,所以,postEvent()传递的事件一定要是动态分配的。
sendEvent()函数直接跳过事件循环队列,直接调用notify()函数发送事件到目标对象,并等待事件处理结果,所以其传递的事件直接在栈上分配即可,(postEvent()函数只负责添加事件到队列,不等待事件处理结果)。
sendEvent调用关系如下:
inline bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event)
{ if (event) event->spont = false; return self ? self->notifyInternal(receiver, event) : false; }
QCoreApplication::notifyInternal(QObject * receiver, QEvent * event)
QCoreApplication::notify(QObject * receiver, QEvent *event)
QCoreApplicationPrivate::notify_helper(QObject * receiver, QE