QT Event Processing -notify ()

Reprinted to: https: //www.deeplearn.me/349.html

I. Description

  Qt one way to handle the event: "inherits QApplication and reimplement notify () function." Qt calls QApplication to send an event to reimplement notify () function before all the events get their only way to get in the event filter. Event filters use more convenient. Because there can be multiple event filters at the same time. And notify () function only one.

Second, the use of function

  Reimplement QApplication class MyApplication header file myapplication.h as follows:

#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 file as follows:

#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 file as follows:

#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 file as follows:

#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)          // response button and MouseButtonRelease event of MouseButtonPress 
    {
         IF (E-> of the 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 file as follows:

#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);   // is a registered QApplication filter 
    mainwindow.setWindowTitle (QObject :: TR ( " inherited QApplication and reimplement notify () function " ));
    mainwindow.resize(400, 200);
    mainwindow.show();
 
    return a.exec();
}

  Run the program, you can either click on the find button, release or drag the mouse will not display any text. Because we have to subclass QApplication, events before reaching the event filters QApplication, and will first reach the QApplication notify () function, we have blocked MouseButtonPress, MouseButtonRelease events subclass of MyApplication in. So registered for the MyApplication object event filter does not work. The program's interface is:

 

Guess you like

Origin www.cnblogs.com/wenhao-Web/p/12292340.html