QT事件处理–notify()
转载至: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()的更多相关文章
- Qt事件处理机制
研一的时候开始使用Qt,感觉用Qt开发图形界面比MFC的一套框架来方便的多.后来由于项目的需要,也没有再接触Qt了.现在要重新拾起来,于是要从基础学起. Now,开始学习Qt事件处理机制. 先给出原文 ...
- QT事件过滤器(QT事件处理的5个层次:自己覆盖或过滤,父窗口过滤,Application过滤与通知)
Qt事件模型一个真正强大的特色是一个QObject 的实例能够管理另一个QObject 实例的事件. 让我们试着设想已经有了一个CustomerInfoDialog的小部件.CustomerInfoD ...
- 【转】Qt 事件处理机制 (下篇)
转自:http://mobile.51cto.com/symbian-272816.htm 在Qt中,事件被封装成一个个对象,所有的事件均继承自抽象类QEvent. 接下来依次谈谈Qt中有谁来产生.分 ...
- 9、Qt 事件处理机制
原文地址:http://mobile.51cto.com/symbian-272812.htm 在Qt中,事件被封装成一个个对象,所有的事件均继承自抽象类QEvent. 接下来依次谈谈Qt中有谁来产生 ...
- Qt 事件处理的五个层次
看了这篇文章(见http://devbean.blog.51cto.com/448512/231861),然后经过自己的思考,把Qt事件处理的五个层次.同时也是Qt时间处理的流程画了出来.若有不对请批 ...
- Qt 事件处理机制
Qt 事件处理机制 因为这篇文章写得特别好,将Qt的事件处理机制能够阐述的清晰有条理,并且便于学习.于是就装载过来了(本文做了排版,并删减了一些冗余的东西,希望原主勿怪),以供学习之用. 简介 在Qt ...
- QT开发(十二)——QT事件处理机制
一.QT事件简介 QT程序是事件驱动的, 程序的每个动作都是由内部某个事件所触发.QT事件的发生和处理成为程序运行的主线,存在于程序整个生命周期. 常见的QT事件类型如下: 键盘事件: 按键按下和松开 ...
- Qt 事件处理机制 (下篇)
继续我们上一篇文章继续介绍,Qt 事件处理机制 (上篇) 介绍了Qt框架的事件处理机制:事件的产生.分发.接受和处理,并以视窗系统鼠标点击QWidget为例,对代码进行了剖析,向大家分析了Qt框架如何 ...
- 【转】解读Qt 事件处理机制(上篇)
[转自]:http://mobile.51cto.com/symbian-272812.htm 在Qt中,事件被封装成一个个对象,所有的事件均继承自抽象类QEvent. 接下来依次谈谈Qt中有谁来产生 ...
随机推荐
- IntelliJ IDEA Debug模式启动项目
导入项目,没有看到debug模式 点击 next next next next
- PyCharm破解安装方法
1.在3322下载站下好压缩包之后,直接点击安装文件“pycharm-professional-2018.1.exe”进行安装,默认点击“next”下一步进行操作2.选择文件所创建的位置.位置可以选择 ...
- JAVA高级编程(数据源datasource)
数据源:通过jdbc连接数据库,多建立几条连接放在数据源里面.可以设置数据源的最大连接数,同时活跃的连接数,最少空闲的连接数,能够同时接收处理的连接数等等. dbcp数据源 需要的jar包: comm ...
- 基于TF-IDF的推荐
仅作学习使用 基于TF-IDF的推荐: 将文档分词 对于每个term,计算词频TF和逆文本指数IDF,形成term的权重 计算项目文档和用户偏好文档的相似度 参考: https://blog.csdn ...
- [ERROR] error: error while loading <root>, error in opening zip file error: scala.reflect.internal.MissingRequirementError: object scala.runtime in compiler mirror not found.
在家编译一个Apache的开源项目,在编译时遇到错误如下: error: error while loading <root>, error in opening zip file [ER ...
- STM32CubeIDE printf 串口重定向
- 常见加密解密 -- pycryptodomex库
安装 windows pip install pycryptodomex ubuntu pip install pycryptodome 加密方式 单向加密:MD5 只能对数据进行加密,而不能解密 对 ...
- Lesson 47 The great escape
What is one of the features of modern camping where nationality is concerned? Economy is one powerfu ...
- 学习Linux系统永远都不晚
作为一名机械专业毕业的学生,两年的工作经历实实在在地教会了我如何认清现实,让当初那个对机械行业无比憧憬的少年明白了自己选择的路有多艰难.由于我的父母都是工人,所以我比其他同龄人能更早地接触到工业的魅力 ...
- 编程题目: 两个队列实现栈(Python)
感觉两个队列实现栈 比 两个栈实现队列 麻烦 1.栈为空:当两个队列都为空的时候,栈为空 2.入栈操作:当队列2为空的时候,将元素入队到队列1:当队列1位空的时候,将元素入队到队列2: 如果队列1 和 ...