鱼C论坛

 找回密码
 立即注册
查看: 2596|回复: 0

[技术原创] 83-事件处理(notify())-【重要基功】

[复制链接]
发表于 2016-10-14 11:16:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 不二如是 于 2016-10-23 15:56 编辑

0.jpg


一个事件由一个特定的QEvent子类来表示,但是有时一个特定QEvent子类来表示,但是有时一个事件又包含多个事件类型。

比如鼠标事件又可以分为鼠标按下、单击、双击、移动等操作。

这些事件类型都由QEvent类的枚举型QEvent::Type来表示,其中包含了一百多种事件类型,QEvent帮助文档一查便知。

虽然QEvent的子类可以表示一个事件,但是却不能用来处理时间,那么应该怎么处理一个事件呢?

那就要说QCoreApplication类的notify()函数了,没错就是最上面那幅图,那里面说的啥:

There are five different ways that events can be processed; reimplementing this virtual function is just one of them. All five approaches are listed below:
1.Reimplementing paintEvent(), mousePressEvent() and so on. This is the commonest, easiest and least powerful way.

2.Reimplementing this function. This is very powerful, providing complete control; but only one subclass can be active at a time.

3.Installing an event filter on QCoreApplication::instance(). Such an event filter is able to process all events for all widgets, so it's just as powerful as reimplementing notify(); furthermore, it's possible to have more than one application-global event filter. Global event filters even see mouse events for disabled widgets. Note that application event filters are only called for objects that live in the main thread.

4.Reimplementing QObject::event() (as QWidget does). If you do this you get Tab key presses, and you get to see the events before any widget-specific event filters.

5.Installing an event filter on the object. Such an event filter gets all the events, including Tab and Shift+Tab key press events, as long as they do not change the focus widget.

哈哈,别着急,我给你翻译下哈:


  • 方法一

    重新实现部件的paintEvent()、mousePressEvent()等事件处理函数。

    这种方法是最常用的一种方法,不过他只能用来处理特定部件的特定事件。

  • 方法二

    重新实现notify()函数。

    这个函数功能强大,提供了完全的控制,可以在事件过滤器得到事件之前就能获得它们。

    但是,他一次处理一个事件。

  • 方法三

    向QApplication对象上安装事件过滤器。

    因为一个程序员只有一个QApplication对象,所以这样实现的功能与使用notify()函数是相同的。‘

    优点是,可以同时处理多个事件。

  • 方法四

    重新实现event()函数。

    QObject类的event()函数可以在事件,到达默认的事件处理函数之前获得该事件。

  • 方法五

    在对象上安装事件过滤器。

    使用事件过滤器可以在一个界面类中同时处理不同子部件的不同事件。


在实际开发中,常用方法一、方法五。

事件二、三虽然功能强大,却很少用到。因为一个继承自QApplication类,一个要用全局的事件过滤器

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-3 02:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表