zoukankan      html  css  js  c++  java
  • 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 = 0);
        ~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(400, 200);
        mainwindow.show();
     
        return a.exec();
    }

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

  • 相关阅读:
    React元素渲染
    初识JSX
    微信小程序复制文本到剪切板
    微信小程序报错request:fail url not in domain list
    小程序,通过自定义编译条件,模拟推荐人功能
    积分抵扣逻辑
    微信小程序 switch 样式
    tomcat 配置开启 APR 模式
    tomcat8 传输json 报错 Invalid character found in the request target. The valid characters are defined in RFC 3986
    c++数组初始化误区
  • 原文地址:https://www.cnblogs.com/wenhao-Web/p/12292340.html
Copyright © 2011-2022 走看看