QT用过滤器实现鼠标的左击,右击,双击,实现不同功能

该死的公司不能联网,不能带自己一切外设,只能在家写好发到这明天来抄

效果展示:左击显示A,右击显示B,双击删除AB

 完整的.h文件

#pragma once

#include <QtWidgets/QWidget>
#include "ui_Day8_Test.h"
#include<qtimer.h>
class Day8_Test : public QWidget
{
    Q_OBJECT

public:
    Day8_Test(QWidget *parent = nullptr);
    ~Day8_Test();
    //QTimer* timer;   //定时器
    bool eventFilter(QObject* watched, QEvent* event);
    void mousePressEvent(QObject* watched, QMouseEvent* event);
signals:
    //void clicked();
public slots:
    //void slotClicked();     //槽函数
protected:
    //void mousePressEvent(QMouseEvent* event);      //单击
    //void mouseDoubleClickEvent(QMouseEvent* event); //双击
private:
    Ui::Day8_TestClass ui;
};

完整的.cpp文件

#pragma execution_character_set("utf-8");
#include "Day8_Test.h"
#include<qtimer.h>
#include <qdebug.h>
#include<QMouseEvent>
#include<QMessageBox>
#include <QEvent>
int state = 0;
Day8_Test::Day8_Test(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
    ui.pushButton_2->hide();
    ui.pushButton_3->hide();
    ui.pushButton_6->hide();
    ui.pushButton_8->hide();
    ui.pushButton_10->hide();
    ui.pushButton_12->hide();
    ui.pushButton_14->hide();
    ui.pushButton_16->hide();
    //给需要的按钮安装过滤器
    ui.pushButton->installEventFilter(this);
    ui.pushButton_7->installEventFilter(this);
    ui.pushButton_11->installEventFilter(this);
    ui.pushButton_15->installEventFilter(this);
    ui.pushButton_2->installEventFilter(this);
    ui.pushButton_3->installEventFilter(this);
    ui.pushButton_6->installEventFilter(this);
    ui.pushButton_8->installEventFilter(this);
    ui.pushButton_10->installEventFilter(this);
    ui.pushButton_12->installEventFilter(this);
    ui.pushButton_14->installEventFilter(this);
    ui.pushButton_16->installEventFilter(this);
}

Day8_Test::~Day8_Test()
{}
//void Day8_Test::slotClicked()
//{
//    timer->stop();
//    QMessageBox::information(NULL, QString::fromLocal8Bit("Click"),
//        QString::fromLocal8Bit("This is click."), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
//
//}
//void Day8_Test::mousePressEvent(QMouseEvent* event)
//{
//    timer->start(200);
//}
//
//void Day8_Test::mouseDoubleClickEvent(QMouseEvent* event)
//{
//    timer->stop();
//    QMessageBox::information(NULL, QString::fromLocal8Bit("DoubleClick"), QString::fromLocal8Bit("This is DoubleClick."), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
//
//}
bool Day8_Test::eventFilter(QObject* watched, QEvent* event)
{
    if (watched == ui.pushButton || ui.pushButton_7 || ui.pushButton_11 || ui.pushButton_15)
    {
        
        if (event->type() == QMouseEvent::MouseButtonPress)
        {
            QMouseEvent* e2 = static_cast<QMouseEvent*>(event);
            if (e2->button() == Qt::LeftButton)
            {
                if (watched == ui.pushButton)
                {
                    ui.pushButton_2->show();
                }
                else if (watched == ui.pushButton_7)
                {
                    ui.pushButton_8->show();
                }
                else if (watched == ui.pushButton_11)
                {
                    ui.pushButton_12->show();
                }
                else if (watched == ui.pushButton_15)
                {
                    ui.pushButton_16->show();
                }
            }
            else if (e2->button() == Qt::RightButton)
            {
                if (watched == ui.pushButton)
                {
                    ui.pushButton_3->show();
                    ui.pushButton->setChecked(true);
                }

                else if (watched == ui.pushButton_7)
                {
                    ui.pushButton_6->show();
                    ui.pushButton_7->setChecked(true);
                }
                else if (watched == ui.pushButton_11)
                {
                    ui.pushButton_10->show();
                    ui.pushButton_11->setChecked(true);
                }
                else if (watched == ui.pushButton_15)
                {
                    ui.pushButton_14->show();
                    ui.pushButton_15->setChecked(true);
                }
            }
        }
    }
    if (watched==ui.pushButton_2|| watched == ui.pushButton_3 || watched == ui.pushButton_6 || watched == ui.pushButton_8
        || watched == ui.pushButton_10 || watched == ui.pushButton_12 || watched == ui.pushButton_14 || watched == ui.pushButton_16)
    {
        if (event->type() == QEvent::MouseButtonDblClick)//QEvent::MouseButtonDblClick
        {
            QMouseEvent* e = static_cast<QMouseEvent*>(event);
            if (e->button() == Qt::LeftButton)
            {
                if (watched == ui.pushButton_2)
                {
                    qDebug() << "111111";
                    ui.pushButton_2->hide();
                }
                else if (watched == ui.pushButton_3)
                    ui.pushButton_3->hide();
                else if (watched == ui.pushButton_6)
                    ui.pushButton_6->hide();
                else if (watched == ui.pushButton_8)
                    ui.pushButton_8->hide();
                else if (watched == ui.pushButton_10)
                    ui.pushButton_10->hide();
                else if (watched == ui.pushButton_12)
                    ui.pushButton_12->hide();
                else if (watched == ui.pushButton_14)
                    ui.pushButton_14->hide();
                else if (watched == ui.pushButton_16)
                    ui.pushButton_16->hide();
            }
        }
    }
    return QWidget::eventFilter(watched, event);
}

//void Day8_Test::mousePressEvent(QObject* watched, QMouseEvent* event)
//{
//    QMouseEvent* e = static_cast<QMouseEvent*>(event);
//    if (e->button() == Qt::LeftButton)
//    {
//        if (watched == ui.pushButton)
//            qDebug() << "按钮1点击了左键";
//        else if (watched == ui.pushButton_7)
//            qDebug() << "按钮2点击了左键";
//        else if (watched == ui.pushButton_11)
//            qDebug() << "按钮3点击了左键";
//        else if (watched == ui.pushButton_15)
//            qDebug() << "按钮4点击了左键";
//    }
//    /*if (event->button() == Qt::LeftButton)
//        qDebug() << "mousePressEvent:左键";*/
//    else if (event->button() == Qt::RightButton)
//    {
//        if (watched == ui.pushButton)
//            qDebug() << "按钮1点击了右键";
//        else if (watched == ui.pushButton_7)
//            qDebug() << "按钮2点击了右键";
//        else if (watched == ui.pushButton_11)
//            qDebug() << "按钮3点击了右键";
//        else if (watched == ui.pushButton_15)
//            qDebug() << "按钮4点击了右键";
//    }
//}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值