转载至:https://www.deeplearn.me/349.html

一、说明

  Qt 处理事件的方式之一:”继承 QApplication 并重新实现 notify()函数”。Qt 调用 QApplication 来发送一个事件,重新实现 notify()函数是在事件过滤器得到所有事件之前获得它们的唯一方法。事件过滤器使用更为便利。因为可以同时有多个事件过滤器。而 notify()函数只有一个。

二、函数的使用

  重新实现的 QApplication 类 MyApplication 的头文件 myapplication.h 如下:

#ifndef MYAPPLICATION_H
#define MYAPPLICATION_H #include <QApplication>
#include <QEvent> class MyApplication : public QApplication
{
public:
MyApplication(int & argc, char ** argv); public:
bool notify(QObject *receiver, QEvent *e);
}; #endif

  myapplication.cpp 文件如下:

#include "myapplication.h"
#include <QMouseEvent> MyApplication::MyApplication(int & argc, char ** argv) : QApplication(argc, argv)
{
} bool MyApplication::notify(QObject *receiver, QEvent *e)
{
//屏蔽 MouseButtonPress、MouseButtonRelease 和 MouseMove 事件
if (e->type() == QEvent::MouseButtonPress || e->type() == QEvent::MouseButtonRelease || e->type() == QEvent::MouseMove)
{
return true;
}
return QApplication::notify(receiver, e);
}

  mainwindow.h 文件如下:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QtGui/QMainWindow>
#include <QPushButton>
#include <QMouseEvent> class MainWindow : public QMainWindow
{
Q_OBJECT public:
MainWindow(QWidget *parent = );
~MainWindow();
protected:
bool eventFilter(QObject *obj, QEvent *e);
private:
QPushButton *button;
}; #endif

  mainwindow.cpp 文件如下:

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
button = new QPushButton;
this->setCentralWidget(button);
} MainWindow::~MainWindow()
{ } bool MainWindow::eventFilter(QObject *obj, QEvent *e)
{
if (obj == button) //响应 button 的 MouseButtonPress 和 MouseButtonRelease 事件
{
if (e->type() == QEvent::MouseButtonPress)
{
QMouseEvent *event = static_cast<QMouseEvent*> (e);
button->setText(QString("Press: %1, %2").arg(QString::number(event->x()), QString::number(event->y())));
return true;
}
else if (e->type() == QEvent::MouseButtonRelease)
{
QMouseEvent *event = static_cast<QMouseEvent*> (e);
button->setText(QString("Release: %1, %2").arg(QString::number(event->x()), QString::number(event->y())));
return true;
}
else
{
return false;
}
} return QMainWindow::eventFilter(obj, e);
}

  main.cpp 文件如下:

#include <QtGui/QApplication>
#include <QtCore/QTextCodec>
#include "mainwindow.h"
#include "myapplication.h" int main(int argc, char *argv[])
{
MyApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030"));
MainWindow mainwindow;
a.installEventFilter(&mainwindow); //为 QApplication 注册过滤器
mainwindow.setWindowTitle(QObject::tr("继承 QApplication 并重新实现 notify()函数"));
mainwindow.resize(, );
mainwindow.show(); return a.exec();
}

  运行程序,可以发现 button 不管是点击、释放还是拖动鼠标,都不会显示任何文本。因为我们已经子类化 QApplication,事件在到达 QApplication 的事件过滤器之前,会先到达 QApplication 的 notify()函数,我们已经在子类化的 MyApplication 中屏蔽了 MouseButtonPress、MouseButtonRelease 事件。所以为 MyApplication 对象注册的事件过滤器不起作用。程序运行界面为:

QT事件处理–notify()的更多相关文章

  1. Qt事件处理机制

    研一的时候开始使用Qt,感觉用Qt开发图形界面比MFC的一套框架来方便的多.后来由于项目的需要,也没有再接触Qt了.现在要重新拾起来,于是要从基础学起. Now,开始学习Qt事件处理机制. 先给出原文 ...

  2. QT事件过滤器(QT事件处理的5个层次:自己覆盖或过滤,父窗口过滤,Application过滤与通知)

    Qt事件模型一个真正强大的特色是一个QObject 的实例能够管理另一个QObject 实例的事件. 让我们试着设想已经有了一个CustomerInfoDialog的小部件.CustomerInfoD ...

  3. 【转】Qt 事件处理机制 (下篇)

    转自:http://mobile.51cto.com/symbian-272816.htm 在Qt中,事件被封装成一个个对象,所有的事件均继承自抽象类QEvent. 接下来依次谈谈Qt中有谁来产生.分 ...

  4. 9、Qt 事件处理机制

    原文地址:http://mobile.51cto.com/symbian-272812.htm 在Qt中,事件被封装成一个个对象,所有的事件均继承自抽象类QEvent. 接下来依次谈谈Qt中有谁来产生 ...

  5. Qt 事件处理的五个层次

    看了这篇文章(见http://devbean.blog.51cto.com/448512/231861),然后经过自己的思考,把Qt事件处理的五个层次.同时也是Qt时间处理的流程画了出来.若有不对请批 ...

  6. Qt 事件处理机制

    Qt 事件处理机制 因为这篇文章写得特别好,将Qt的事件处理机制能够阐述的清晰有条理,并且便于学习.于是就装载过来了(本文做了排版,并删减了一些冗余的东西,希望原主勿怪),以供学习之用. 简介 在Qt ...

  7. QT开发(十二)——QT事件处理机制

    一.QT事件简介 QT程序是事件驱动的, 程序的每个动作都是由内部某个事件所触发.QT事件的发生和处理成为程序运行的主线,存在于程序整个生命周期. 常见的QT事件类型如下: 键盘事件: 按键按下和松开 ...

  8. Qt 事件处理机制 (下篇)

    继续我们上一篇文章继续介绍,Qt 事件处理机制 (上篇) 介绍了Qt框架的事件处理机制:事件的产生.分发.接受和处理,并以视窗系统鼠标点击QWidget为例,对代码进行了剖析,向大家分析了Qt框架如何 ...

  9. 【转】解读Qt 事件处理机制(上篇)

    [转自]:http://mobile.51cto.com/symbian-272812.htm 在Qt中,事件被封装成一个个对象,所有的事件均继承自抽象类QEvent. 接下来依次谈谈Qt中有谁来产生 ...

随机推荐

  1. Tensorflow机器学习入门——网络可视化TensorBoard

    一.在代码中标记要显示的各种量 tensorboard各函数的作用和用法请参考:https://www.cnblogs.com/lyc-seu/p/8647792.html import tensor ...

  2. Flask - 上下文管理(核心)

    参考 http://flask.pocoo.org/docs/1.0/advanced_foreword/#thread-locals-in-flask https://zhuanlan.zhihu. ...

  3. Tensorflow之AttributeError: module 'tensorflow' has no attribute 'mul'

      tf.mul已经在新版本中被移除,请使用 tf.multiply 代替

  4. git安装以及gitlib配置

    安装Git:详见http://www.cnblogs.com/xiuxingzhe/p/9300905.html 开通gitlab(开通需要咨询所在公司的gitlab管理员)账号后,本地Git仓库和g ...

  5. 使用anaconda 3安装tensorflow 1.15.0 (win10环境)

    0.写在前面 ​ 之前其实安装过一次tensorflow,但是由于电脑中毒,重装了系统,把所有的环境全部删除了.之前在博客里转发了一篇别人在win10安装tensorflow的教程,但是版本比较旧了, ...

  6. @Qualifier

    当一个接口,有多个实现类且均已注入到spring容器中了,使用时@AutoWired是byType的,而这些实现类类型都相同,此时就需要使用@Qualifier明确指定使用那个实现类.因此,@Qual ...

  7. 十六、JSONObject与JSONArray使用-不刷新页面做回写显示

    需要导入:json-lib-2.2.2-.jar包 1.json:就是一个键对应一个值,超级简单的一对一关系.对于json嵌套,只要记住符号“:”前是键,符号后是值大括号成对找. String arr ...

  8. VUE框架下安装自带http协议

    在控制台CMD 中输入 npm install vue-resource --save-dev

  9. 关于C/C++的各种优化

    一.常量 声明常量可以方便代码的修改,提高复用性. ; +; ; 同时,声明常量也可以减少重复运算,提高代码速度,例子如下: string s; cin>>s; ;i<len;i++ ...

  10. android 根据res文件夹下(如res/raw)文件名获取其id

    android 根据res文件夹下(如res/raw)文件名获取其id //测试是否能够获取其资源ID int treeId = mv.getResources().getIdentifier(fil ...