Qt中事件分发源代码剖析

Qt中事件传递顺序:

在一个应该程序中,会进入一个事件循环,接受系统产生的事件,并且进行分发,这些都是在exec中进行的。
下面举例说明:

1)首先看看下面一段示例代码:

  1. int main(int argc, char *argv[])
  2. {
  3. QApplication a(argc, argv);
  4. MouseEvent w;
  5. w.show();
  6. return a.exec();
  7. }

2)a.exec进入事件循环,调用的是QApplication::exec();

  1. int QApplication::exec()
  2. {
  3. return <span style="color:#ff6666;">QGuiApplication::exec();</span>
  4. }

3)QApplication::exec()调用的是QGuiApplication::exec();

  1. int QGuiApplication::exec()
  2. {
  3. #ifndef QT_NO_ACCESSIBILITY
  4. QAccessible::setRootObject(qApp);
  5. #endif
  6. return QCoreApplication::exec();
  7. }

4)QGuiApplication::exec()调用的是QCoreApplication::exec();

  1. int QCoreApplication::exec()
  2. {
  3. if (!QCoreApplicationPrivate::checkInstance("exec"))
  4. return -1;
  5. QThreadData *threadData = self->d_func()->threadData;
  6. if (threadData != QThreadData::current()) {
  7. qWarning("%s::exec: Must be called from the main thread", self->metaObject()->className());
  8. return -1;
  9. }
  10. if (!threadData->eventLoops.isEmpty()) {
  11. qWarning("QCoreApplication::exec: The event loop is already running");
  12. return -1;
  13. }
  14. threadData->quitNow = false;
  15. QEventLoop eventLoop;
  16. self->d_func()->in_exec = true;
  17. self->d_func()->aboutToQuitEmitted = false;
  18. int returnCode = eventLoop.exec();
  19. threadData->quitNow = false;
  20. if (self) {
  21. self->d_func()->in_exec = false;
  22. if (!self->d_func()->aboutToQuitEmitted)
  23. emit self->aboutToQuit(QPrivateSignal());
  24. self->d_func()->aboutToQuitEmitted = true;
  25. sendPostedEvents(0, QEvent::DeferredDelete);
  26. }
  27. return returnCode;
  28. }

5)QCoreApplication::exec()调用eventLoop.exec()进行事件循环;

  1. int QEventLoop::exec(ProcessEventsFlags flags)
  2. {
  3. Q_D(QEventLoop);
  4. //we need to protect from race condition with QThread::exit
  5. QMutexLocker locker(&static_cast<QThreadPrivate *>(QObjectPrivate::get(d->threadData->thread))->mutex);
  6. if (d->threadData->quitNow)
  7. return -1;
  8. if (d->inExec) {
  9. qWarning("QEventLoop::exec: instance %p has already called exec()", this);
  10. return -1;
  11. }
  12. struct LoopReference {
  13. QEventLoopPrivate *d;
  14. QMutexLocker &locker;
  15. bool exceptionCaught;
  16. LoopReference(QEventLoopPrivate *d, QMutexLocker &locker) : d(d), locker(locker), exceptionCaught(true)
  17. {
  18. d->inExec = true;
  19. d->exit = false;
  20. ++d->threadData->loopLevel;
  21. d->threadData->eventLoops.push(d->q_func());
  22. locker.unlock();
  23. }
  24. ~LoopReference()
  25. {
  26. if (exceptionCaught) {
  27. qWarning("Qt has caught an exception thrown from an event handler. Throwing\n"
  28. "exceptions from an event handler is not supported in Qt. You must\n"
  29. "reimplement QApplication::notify() and catch all exceptions there.\n");
  30. }
  31. locker.relock();
  32. QEventLoop *eventLoop = d->threadData->eventLoops.pop();
  33. Q_ASSERT_X(eventLoop == d->q_func(), "QEventLoop::exec()", "internal error");
  34. Q_UNUSED(eventLoop); // --release warning
  35. d->inExec = false;
  36. --d->threadData->loopLevel;
  37. }
  38. };
  39. LoopReference ref(d, locker);
  40. // remove posted quit events when entering a new event loop
  41. QCoreApplication *app = QCoreApplication::instance();
  42. if (app && app->thread() == thread())
  43. QCoreApplication::removePostedEvents(app, QEvent::Quit);
  44. while (!d->exit)
  45. processEvents(flags | WaitForMoreEvents | EventLoopExec);
  46. ref.exceptionCaught = false;
  47. return d->returnCode;
  48. }

6)eventLoop.exec()调用QCoreApplication的processEvents进行事件分发;

7)调用notify进行分发

QCoreApplication::sendEvent、QCoreApplication::postEvent和QCoreApplication::sendPostedEvents都调用notify进行事件分发;

  1. bool QCoreApplication::notify(QObject *receiver, QEvent *event)
  2. {
  3. Q_D(QCoreApplication);
  4. // no events are delivered after ~QCoreApplication() has started
  5. if (QCoreApplicationPrivate::is_app_closing)
  6. return true;
  7. if (receiver == 0) {                        // serious error
  8. qWarning("QCoreApplication::notify: Unexpected null receiver");
  9. return true;
  10. }
  11. #ifndef QT_NO_DEBUG
  12. d->checkReceiverThread(receiver);
  13. #endif
  14. return receiver->isWidgetType() ? false :<span style="color:#ff6666;"> d->notify_helper</span>(receiver, event);
  15. }

8)notify调用notify_helper进行事件分发;

  1. bool QCoreApplicationPrivate::notify_helper(QObject *receiver, QEvent * event)
  2. {
  3. // send to all application event filters
  4. if (sendThroughApplicationEventFilters(receiver, event))
  5. return true;
  6. // send to all receiver event filters
  7. if (sendThroughObjectEventFilters(receiver, event))
  8. return true;
  9. // deliver the event
  10. return receiver->event(event);
  11. }

9)从上面第8步的代码可以看出事件传递

传递的顺序是:首先传递给全局的事件过滤器,再传递给目标对象的事件过滤器,最终传递给目标对象。

http://blog.csdn.net/chenlong12580/article/details/25009095

Qt中事件分发源代码剖析(一共8个步骤,顺序非常清楚:全局的事件过滤器,再传递给目标对象的事件过滤器,最终传递给目标对象)的更多相关文章

  1. Qt中事件分发源代码剖析

    Qt中事件分发源代码剖析 Qt中事件传递顺序: 在一个应该程序中,会进入一个事件循环,接受系统产生的事件,并且进行分发,这些都是在exec中进行的. 下面举例说明: 1)首先看看下面一段示例代码: i ...

  2. Qt中事件分发源码剖析

    Qt中事件分发源码剖析 Qt中事件传递顺序: 在一个应该程序中,会进入一个事件循环,接受系统产生的事件,而且进行分发,这些都是在exec中进行的. 以下举例说明: 1)首先看看以下一段演示样例代码: ...

  3. 关于JAVA中事件分发和监听机制实现的代码实例-绝对原创实用

    http://blog.csdn.net/5iasp/article/details/37054171 文章标题:关于JAVA中事件分发和监听机制实现的代码实例 文章地址: http://blog.c ...

  4. Android与javascript中事件分发机制的简单比较

    在前面两篇博客中,我们讨论了Android中的事件分发的相关内容,那么在本篇博客当中,我们就简单探讨一下html或javascript中的事件分发机制,并进行简单的对比. 在前端中,对事件进行绑定有三 ...

  5. QT中事件处理器和事件过滤器实现实例

    Qt中事件处理的方式,最常用的就是使用事件处理器(event handler)和事件过滤器(event filter)这两种方法.接下来,我们就来看看事件处理器和事件过滤器是怎么使用的. 事件处理器 ...

  6. JDK1.7中的ThreadPoolExecutor源代码剖析

    JDK1. 7中的ThreadPoolExecutor 线程池,顾名思义一个线程的池子,池子里存放了非常多能够复用的线程,假设不用线程池相似的容器,每当我们须要创建新的线程时都须要去new Threa ...

  7. Qt 中事件与处理

    一.事件与处理程序在运算过程中发生的一些事情:鼠标单击.键盘的按下...这些的事件的监控与处理在Qt中不是以信号的方式处理的.当这些事件发生时会调用QObject类中的功能函数(虚函数),所有的控件类 ...

  8. Android事件分发机制浅谈(二)--源码分析(ViewGroup篇)

    上节我们大致了解了事件分发机制的内容,大概流程,这一节来分析下事件分发的源代码. 我们先来分析ViewGroup中dispatchTouchEvent()中的源码 public boolean dis ...

  9. 第39课 Qt中的事件处理(下)

    1. 事件的传递过程 (1)操作系统检测到用户动作时,会产生一条系统消息,该消息被发送到Qt应用程序 (2)Qt应用程序收到系统消息后,将其转化为一个对应的QEvent事件对象,并调用QObject: ...

随机推荐

  1. C# LinkButton 带参数的OnCommand事件的写法

    前台: <asp:TemplateField HeaderText ="操作"> <HeaderStyle HorizontalAlign ="Cent ...

  2. fuse 虚拟文件系统 的 安装与使用

    FUSE 是个好东西, 可以用在特殊的领域里面来实现自己的虚拟文件系统. FUSE的下载与安装 : 从fuse官网下载最新安装包:http://sourceforge.net/projects/fus ...

  3. Java获取当前日期的前一个月,前一天的时间

    Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -); //得到前一天 calendar.add(Cal ...

  4. Weekend counter

    Weekend counter Sofia has given you a schedule and two dates and told you she needs help planning he ...

  5. 转:sql语句中GROUP BY 和 HAVING和使用 count()

    在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7   例1:查询出现 ...

  6. linux操作系下RAR的使用

    ============zip文件的操作================================== zip -r data.zip data 解释:将data文件夹压缩成了data.zip格 ...

  7. UML--核心视图之用例图

    如果说UML是一门语言,那么元素就是UML的基本词汇,视图就是语法. UML通过视图将基本元素组织在一起,形成有意义的句子. 静态视图,顾名思义,就是表达静态事物的.包括用例图.类图和包图. 用例图 ...

  8. 什么是内存泄漏?(What is a memory leak?)

    程序中的内存泄漏是怎么回事呢? 我们写过很多带有关键词free()的程序.比如我在这篇博文关于链表的一些重要操作(Important operations on a Linked List)中删除整个 ...

  9. iOS AFNetworking 详解

    1. 很不错的介绍 http://m.blog.csdn.net/blog/jackljf/38736625

  10. 禁用UITableViewCell 重用机制

    有时候不想让Cell重用,怎么办勒.接下来介绍两种方法 方法一 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAt ...