QT event handling, rewriting mouse events, use of event filters

Override mouse click events, mouse release events, and mouse movement events. Event filters implement event processing for a single control.

Rewrite the QLabel class and reimplement the mouse event
mylabel.h

#ifndef MYLABEL_H
#define MYLABEL_H
#include <QLabel>
class MyLabel : public QLabel
{
    
    
    Q_OBJECT
public:
    explicit MyLabel(QWidget *parent = nullptr);
protected:
    //鼠标点击事件
    void mousePressEvent(QMouseEvent *ev);
    //鼠标释放事件
    void mouseReleaseEvent(QMouseEvent *ev);
    //鼠标移动事件
    void mouseMoveEvent(QMouseEvent *ev);
};
#endif // MYLABEL_H

mylabel.cpp

#include "mylabel.h"
#include<QMouseEvent>
#include<QDebug>
#include<QEvent>
MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
{
    
    
    //设置默认追踪鼠标
    this->setMouseTracking(true);
}
void MyLabel::mousePressEvent(QMouseEvent *ev)
{
    
    
    int i = ev->x();
    int j = ev->y();
    //判断鼠标按下
    if(ev->button() == Qt::LeftButton)
    {
    
    
        qDebug() << "left";
    }
    if(ev->button()==Qt::RightButton)
    {
    
    
        qDebug()<<"riget";
    }
    if(ev->button()==Qt::MidButton)
    {
    
    
        qDebug()<<"mid";
    }
    //显示鼠标坐标,居中、加粗
    QString text = QString("<center><h1>Mouse Pressed:(%1,%2)</h1></center>").arg(i).arg(j);
    this->setText(text);
}
void MyLabel::mouseReleaseEvent(QMouseEvent *ev)
{
    
    
    QString text = QString("<center><h1>Mouse Release:(%1,%2)</h1></center>").arg(ev->x()).arg(ev->y());
    this->setText(text);
}
 void MyLabel::mouseMoveEvent(QMouseEvent *ev)
 {
    
    
     QString text = QString("<center><h1>Mouse Move:(%1,%2)</h1></center>").arg(ev->x()).arg(ev->y());
     this->setText(text);
 }

Add a QLabel control to mywidget and upgrade it to MyLabel;
add a checkbox to the interface, set the background to green, and the background color range after layout is large, but only clicking the text area button will respond, clicking the area button without text and background color will not In response, click response is achieved through event filters.
mywidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include"mylabel.h"
namespace Ui {
    
    
class MyWidget;
}
class MyWidget : public QWidget
{
    
    
    Q_OBJECT
public:
    explicit MyWidget(QWidget *parent = nullptr);
    ~MyWidget();
private:
    Ui::MyWidget *ui;
public:
    bool eventFilter(QObject *watched, QEvent *event);//事件过滤器
};
#endif // MYWIDGET_H

mywidget.cpp

#include "mywidget.h"
#include "ui_mywidget.h"
//#include <QKeyEvent>
#include <QDebug>
MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyWidget)
{
    
    
    ui->setupUi(this);
    ui->checkBox->setStyleSheet("background-color :green");
    ui->checkBox->installEventFilter(this);//控件需要注册事件过滤器
}
MyWidget::~MyWidget()
{
    
    
    delete ui;
}
/**
 * @brief 事件过滤器
 * @param watched控件
 * @param event事件
 * @return
 */
bool MyWidget::eventFilter(QObject *watched, QEvent *event)
{
    
    
    if(watched == ui->checkBox)
    {
    
    
        qDebug()<<"["<<__FILE__<<"]"<<__LINE__<<__FUNCTION__<<"checkBox Event "<<event->type();
        if(event->type() == QEvent::MouseButtonRelease)
        {
    
    
            QCheckBox *checkBox = static_cast<QCheckBox*>(watched);
            checkBox->setChecked(!checkBox->isChecked());
            return true;
        }
    }
    return QWidget::eventFilter(watched,event);
}

Guess you like

Origin blog.csdn.net/weixin_40355471/article/details/108605949