包括各种event( key mouse timer pushbutton ),event的处理和忽略。
//mybutton.h
#ifndef MYBUTTON_H
#define MYBUTTON_H
#include <QPushButton>
class MyButton : public QPushButton
{
Q_OBJECT
public:
explicit MyButton(QWidget *parent = nullptr);
protected:
void mousePressEvent(QMouseEvent *e);
signals:
public slots:
};
#endif // MYBUTTON_H
//mybutton.cpp
#include "mybutton.h"
#include <QMouseEvent>
#include <QDebug>
MyButton::MyButton(QWidget *parent) : QPushButton(parent)
{
}
void MyButton::mousePressEvent(QMouseEvent *e)
{
if(e->button() == Qt::LeftButton){
qDebug()<< "按下了左键";
//事件接收后就不会向后传递
e->ignore(); //如果加这句表示接收处理后再忽略,也就是继续向后传递
//但是传给的是父组件(父对象),不是父类
}else{
//此处不做处理,继续向后传递信号
QPushButton::mousePressEvent(e);
}
}
//mylabel.h
#ifndef MYLABEL_H
#define MYLABEL_H
#include <QLabel>
class MyLabel : public QLabel
{
Q_OBJECT
public:
explicit MyLabel(QWidget *parent = nullptr);
protected: //基类的虚函数是保护类型,所以这里也是保护类型
//mouse event
void mousePressEvent(QMouseEvent *event); // 虚函数已经在基类声明,所以会自动提示
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
//进入窗口区域
void enterEvent(QEvent *event);
//离开窗口区域
void leaveEvent(QEvent *event);
signals:
public slots:
};
#endif // MYLABEL_H