3.QT事件处理,消息过滤器

时间:2022-07-26 00:59:46



新建一个项目:06Event

新建cpp文件

06Event.pro

HEADERS
+=
\

MyWidget.h

SOURCES
+=
\

MyWidget.cpp

QT
+=
widgets gui

MyWidget.h

#ifndef
MYWIDGET_H

#define
MYWIDGET_H

#include
<QWidget>

#include
<QPushButton>

#include
<QLineEdit>

class
MyWidget
:
public
QWidget

{

Q_OBJECT

public:

explicit
MyWidget(QWidget
*parent
);

bool
event(QEvent
*);

void
mousePressEvent(QMouseEvent
*);

void
mouseReleaseEvent(QMouseEvent
*);

void
mouseMoveEvent(QMouseEvent
*);

/*不要用,因为它默认调用两次mousePressEvent*/

//void
mouseDoubleClickEvent(QMouseEvent
*);

void
keyPressEvent(QKeyEvent
*);

void
keyReleaseEvent(QKeyEvent
*);

void
closeEvent(QCloseEvent
*);

//void
showEvent(QShowEvent
*);

//void
hideEvent(QHideEvent
*);

void
paintEvent(QPaintEvent
*);

QPushButton*
button;

QLineEdit*
edit;

signals:

public
slots:

void
slotButtonClicked();

};

#endif
//
MYWIDGET_H

MyWidget.cpp

#include "MyWidget.h"
#include <QApplication>
#include <QEvent>
#include <QDebug>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QVBoxLayout>
#include <QPainter>
 
MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent)
{
//  QVBoxLayout* lay = new QVBoxLayout(this);
#if 0
    QPushButton* button2;
    lay->addWidget(button = new QPushButton("OK", this));
    lay->addWidget(button2 = new QPushButton("Button2"));
    //设置它为默认的焦点,当点击tab键之后可以切换焦点
    button->setDefault(true);
 
    // 鼠标不需要按下,mouseMove就能得到调用
    this->setMouseTracking(true);
 
    connect(button2, SIGNAL(clicked()), this, SLOT(slotButtonClicked()));
    connect(button, SIGNAL(clicked()), this, SLOT(slotButtonClicked()));
#endif
//    lay->addWidget(edit = new QLineEdit());
//    connect(edit, SIGNAL(returnPressed()), this, SLOT(slotButtonClicked()));
}
 
//通过这一句实现点击按钮的时候获得按钮上的text()内容
void MyWidget::slotButtonClicked()
{
    QLineEdit* button = (QLineEdit*)sender();
    qDebug() << button->text();
}
 
/* QApplication先得到->具体应该处理的窗口::event()->event()根据消息类型来调用具体的虚函数 */
/* 1)可以重载具体的虚函数,来实现对消息的响应
   2)可以重载event函数,用来处理或者截取消息 */
 
/* 截取消息 */
bool MyWidget::event(QEvent *ev)
{
    // 鼠标消息被截断
   // if(ev->type() == QEvent::MouseButtonPress)
     //   return true;
    ev->accept();
 
    return QWidget::event(ev);
}
 
/**
 * @brief MyWidget::closeEvent 窗口关闭的事件
 */
void MyWidget::closeEvent(QCloseEvent *)
{
    qDebug() << "closeEvent";
}
 
void MyWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    //通过下面的方式实现画线
    p.drawLine(QPoint(0, 0), QPoint(100, 100));
}
 
/**
 * @brief MyWidget::mousePressEvent 鼠标按下的事件
 * @param ev
 */
void MyWidget::mousePressEvent(QMouseEvent *ev)
{
#if 0
    QPoint pt = ev->pos();
    qDebug() << pt;
    //如果鼠标按下的是左键的处理
    if(ev->button() == Qt::LeftButton){}
 
    //如果按下的Shift键了
    if(ev->modifiers() == Qt::ShiftModifier)
    {
        qDebug() << "shift press";
    }
#endif
    //改进办法:先判断是否有左键,然后如果判断是否还按了Ctrl键
    if(ev->button() == Qt::LeftButton)
    {
        if(ev->modifiers() == Qt::ControlModifier)
        {
            // handle with Control;
            return;
        }
        // handle2 without control;
    } else {}
}
 
/**
 * @brief MyWidget::mouseReleaseEvent 鼠标释放的按键
 */
void MyWidget::mouseReleaseEvent(QMouseEvent *){}
 
/**
 * @brief MyWidget::mouseMoveEvent 鼠标移动的事件
 */
void MyWidget::mouseMoveEvent(QMouseEvent *)
{
    static int i=0;
    qDebug() << "mouse move"<< i++;
}
 
/**
 * @brief MyWidget::keyPressEvent 鼠标按下的事件,通过这个可以获得按下的键
 * @param ev
 */
void MyWidget::keyPressEvent(QKeyEvent *ev)
{
    ev->modifiers();
    int key = ev->key();
    qDebug() << key;
    char a = key;
    qDebug() << (char)a;
}
 
/**
 * @brief MyWidget::keyReleaseEvent 按键释放的事件
 */
void MyWidget::keyReleaseEvent(QKeyEvent *){}
 
int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
 
    MyWidget w;
    w.show();
 
    return app.exec();
}
运行结果:
3.QT事件处理,消息过滤器


QT消息过滤器

EventFilter.pro

HEADERS
+=
\

MyWidget.h
\

MyApplication.h

SOURCES
+=
\

MyWidget.cpp
\

MyApplication.cpp

QT
+=
widgets gui

MyWidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H
 
#include <QWidget>
#include <QPushButton>
class MyWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MyWidget(QWidget *parent);
 
    QPushButton* _button;
    bool eventFilter(QObject *, QEvent *);
    bool event(QEvent *);
signals:
 
public slots:
 
};
 
#endif // MYWIDGET_H

MyWidget.cpp

#include "MyWidget.h"
#include <QPushButton>
#include <QEvent>
#include "MyApplication.h"
#include <QDebug>
#include <QApplication>
 
MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent)
{
    QPushButton* button;
 
    button = new QPushButton("This button", this);
    connect(button, SIGNAL(clicked()), this, SLOT(close()));
    _button = button;
 
    /*button给自己安装了一个消息过滤器,那么经过button的消息,
     * 都先要调用它的过滤器的eventFilter函数*/
    button->installEventFilter(this);
}
 
bool MyWidget::eventFilter(QObject *o, QEvent *e)
{
#if 0
    if(0 == (QObject*)_button &&(
                e->type() == QEvent::MouseButtonRelease ||
                e->type() == QEvent::MouseButtonDblClick ||
                e->type() == QEvent::MouseButtonPress)) {
        return true;
    }
#endif
 
    return QWidget::eventFilter(o,e);
}
 
bool MyWidget::event(QEvent *e)
{
    if(e->type() == QEvent::User)
    {
        qDebug() << "User event is comming";
    }
    return QWidget::event(e);
}
 
int main(int argc,char *argv[])
{
    MyApplication app(argc,argv);
    MyWidget w;
    w.show();
 
    //发送一个Event给MyWidget
    qDebug() << "begin send";
    app.postEvent(&w,new QEvent(QEvent::User));
    qDebug() << "end send";
 
    return app.exec();
}

MyApplication.h

#ifndef MYAPPLICATION_H
#define MYAPPLICATION_H
 
#include <QApplication>
 
class MyApplication : public QApplication
{
    Q_OBJECT
public:
    MyApplication(int argc,char *argv[]):QApplication(argc,argv)
    {}
    bool notify(QObject *,QEvent *);
 
signals:
 
public slots:
 
};
 
#endif // MYAPPLICATION_H

MyApplication.cpp

#include "MyApplication.h"
#include <QEvent>
 
#include <QDebug>
bool MyApplication::notify(QObject *o, QEvent *e)
{
    if(this->topLevelWidgets().count()>0)
    {
        QWidget* mainWnd = this->topLevelWidgets().at(0);
        if(o==(QObject*)mainWnd && e->type() == QEvent::MouseButtonPress)
        {
            // do ...
            qDebug() << "mainwnd is clicked";
        }
    }
 
    return QApplication::notify(o, e);
}

运行结果:

3.QT事件处理,消息过滤器