Qt---事件

一、Qt中的事件

鼠标事件
        鼠标进入事件enterEvent

        鼠标离开事件leaveEvent

        鼠标按下mousePressEvent ( QMouseEvent ev)
        鼠标释放mouseReleaseEvent
        鼠标移动mouseMoveEvent

ev->x():×坐标        ev->y():y坐标
ev->button()可以判断所有按键Qt::LeftButton        Qt::RightButton
ev->buttons()判断组合按键,判断move时候的左右键,结合&操作符

格式化字符串        Qstring(“ %1 %2" ).arg(111 ).arg(222)
设置鼠标追踪        setMouseTracking(true);

代码示例:

mylabel.h

#ifndef MYLABEL_H
#define MYLABEL_H

#include <QLabel>

class myLabel : public QLabel
{
    Q_OBJECT
public:
    explicit myLabel(QWidget *parent = nullptr);

    //鼠标进入事件
    void enterEvent(QEvent *event);

    //鼠标离开事件
    void leaveEvent(QEvent *);

    //鼠标按下
    void mousePressEvent(QMouseEvent *ev);

    //鼠标释放
    void mouseReleaseEvent(QMouseEvent *ev);

    //鼠标移动
    void mouseMoveEvent(QMouseEvent *ev);

signals:

};

#endif // MYLABEL_H

mylabel.cpp

#include "mylabel.h"
#include<QDebug>
#include<QMouseEvent>

myLabel::myLabel(QWidget *parent) : QLabel(parent)
{
    //设置鼠标追踪
    setMouseTracking(true);
}

//鼠标进入事件
void myLabel::enterEvent(QEvent *event)
{
    //qDebug()<<"鼠标进入了";
}

//鼠标离开事件
void myLabel::leaveEvent(QEvent *event)
{
    //qDebug()<<"鼠标离开了";
}

//鼠标按下
void myLabel::mousePressEvent(QMouseEvent *ev)
{
    //当鼠标左键按下 提示信息
//    if (ev->button() == Qt::LeftButton)
//    {
        QString str = QString("鼠标按下了 x = %1 y = %2  globalX = %3 globalY = %4").arg(ev->x()).arg(ev->y()).arg(ev->globalX()).arg(ev->globalY());
        qDebug()<<str;
//    }
}

//鼠标释放
void myLabel::mouseReleaseEvent(QMouseEvent *ev)
{
//    if (ev->button() == Qt::LeftButton)
//    {
        QString str = QString("鼠标释放了 x = %1 y = %2  globalX = %3 globalY = %4").arg(ev->x()).arg(ev->y()).arg(ev->globalX()).arg(ev->globalY());
        qDebug()<<str;
//    }
}

//鼠标移动
void myLabel::mouseMoveEvent(QMouseEvent *ev)
{
//    if (ev->buttons() & Qt::LeftButton)
//    {
        QString str = QString("鼠标移动了 x = %1 y = %2  globalX = %3 globalY = %4").arg(ev->x()).arg(ev->y()).arg(ev->globalX()).arg(ev->globalY());
        qDebug()<<str;
//    }
}

输出如下所示:

二、定时器

定时器1

        利用事件void timerEvent ( QTimerEvent* ev)

        启动定时器startTimer( 1000)毫秒单位
        timerEvent的返回值是定时器的唯一标识可以和ev->timerld做比较

定时器2
        利用定时器类QTimer
        创建定时器对象QTimer * timer = new QTimer(this)

        启动定时器timer->start(毫秒)
        每隔一定毫秒,发送信号timeout,进行监听

        暂停timer->stop

代码示例:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

    //重写定时器事件
    void timerEvent(QTimerEvent *);

    int id1;//定时器1的唯一标识
    int id2;//定时器2的唯一标识

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include<QTimer>//定时器的类
#include<QPushButton>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //启动定时器
    id1 = startTimer(1000);//参数1 间隔 单位为毫秒

    id2 = startTimer(2000);

    //定时器第二种方式
    QTimer *timer = new QTimer(this);
    //启动定时器
    timer->start(500);

    connect(timer,&QTimer::timeout,[=](){
        static int num = 1;

        //laebl_4每隔0.5秒+1
        ui->label_4->setText(QString::number(num++));
    });

//    //点击暂停按钮,实现停止定时器
//    connect(ui->btn1,&QPushButton::clicked,[=](){
//        timer->stop();
//    });

//    //点击开始按钮,实现开始定时器
//    connect(ui->btn2,&QPushButton::clicked,[=](){
//        timer->start();
//    });

    //一个按钮实现暂停和开始定时器
    connect(ui->btn1,&QPushButton::clicked,[=](){
        if(ui->btn1->text()=="暂停")
        {
            ui->btn1->setText("开始");
            timer->stop();
        }
        else if(ui->btn1->text()=="开始")
        {
            ui->btn1->setText("暂停");
            timer->start();
        }

    });

}

void Widget::timerEvent(QTimerEvent *ev)
{
    if(ev->timerId() == 1)
    {
        static int num = 1;

        //laebl_2每隔1秒+1
        ui->label_2->setText(QString::number(num++));
    }


    if(ev->timerId() == 2)
    {
        static int num2 = 1;

        //laebl_3每隔2秒+1
        ui->label_3->setText(QString::number(num2++));
    }

}

Widget::~Widget()
{
    delete ui;
}

输出如下所示:(单个按钮和两个按钮实现同信号和槽部分练习)

三、event事件分发器

用途:用于事件的分发,也可以做拦截操作,不建议

bool event( QEvent*e); 
返回值如果是true 代表用户处理这个事件,不向下分发了

e->type()== 鼠标按下

代码示例:

bool myLabel::event(QEvent *e)
{
    //如果是鼠标按下,在event事件分发中做拦截
    if(e->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *ev = static_cast<QMouseEvent *>(e);//把QEvent *e转为QMouseEvent *ev
        QString str = QString("Event函数中:鼠标按下了 x = %1 y = %2  globalX = %3 globalY = %4").arg(ev->x()).arg(ev->y()).arg(ev->globalX()).arg(ev->globalY());
        qDebug()<<str;

        return true;//true代表用户自己处理这个事件,不向下分发
    }

//    if(e->type() == QEvent::MouseButtonRelease)
//        {
//            qDebug()<<"Event函数中:鼠标释放事件";
//            return true;
//        }

    //其他时间交给父类处理->默认处理
    return QLabel::event(e);
}

输出如下所示:

四、事件过滤器

在程序将时间分发到事件分发器前,可以利用过滤器做拦截

步骤
        1. 给控件安装事件过滤器
        2. 重写eventFilter函数(obj , ev)

代码示例:

#include "widget.h"
#include "ui_widget.h"
#include<QTimer>//定时器的类
#include<QPushButton>
#include<QMouseEvent>
#include<QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //启动定时器
    id1 = startTimer(1000);//参数1 间隔 单位为毫秒

    id2 = startTimer(2000);

    //定时器第二种方式
    QTimer *timer = new QTimer(this);
    //启动定时器
    timer->start(500);

    connect(timer,&QTimer::timeout,[=](){
        static int num = 1;

        //laebl_4每隔0.5秒+1
        ui->label_4->setText(QString::number(num++));
    });

    //点击暂停按钮,实现停止定时器
    connect(ui->btn1,&QPushButton::clicked,[=](){
        timer->stop();
    });

//    //点击开始按钮,实现开始定时器
//    connect(ui->btn2,&QPushButton::clicked,[=](){
//        timer->start();
//    });

//    //一个按钮实现暂停和开始定时器
//    connect(ui->btn1,&QPushButton::clicked,[=](){
//        if(ui->btn1->text()=="暂停")
//        {
//            ui->btn1->setText("开始");
//            timer->stop();
//        }
//        else if(ui->btn1->text()=="开始")
//        {
//            ui->btn1->setText("暂停");
//            timer->start();
//        }

//    });

    //给label1安装事件过滤器
    //步骤1 安装事件过滤器
    ui->label->installEventFilter(this);
}

//步骤2   重写eventfilter事件
bool Widget::eventFilter(QObject *obj,QEvent *e)
{
    if(obj == ui->label)
    {
        if(e->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *ev = static_cast<QMouseEvent *>(e);//把QEvent *e转为QMouseEvent *ev
            QString str = QString("事件过滤器中:鼠标按下了 x = %1 y = %2  globalX = %3 globalY = %4").arg(ev->x()).arg(ev->y()).arg(ev->globalX()).arg(ev->globalY());
            qDebug()<<str;

            return true;//true代表用户自己处理这个事件,不向下分发
        }
    }

    //其他默认处理
    return QWidget::eventFilter(obj,e);
}

void Widget::timerEvent(QTimerEvent *ev)
{
    if(ev->timerId() == 1)
    {
        static int num = 1;

        //laebl_2每隔1秒+1
        ui->label_2->setText(QString::number(num++));
    }


    if(ev->timerId() == 2)
    {
        static int num2 = 1;

        //laebl_3每隔2秒+1
        ui->label_3->setText(QString::number(num2++));
    }

}

Widget::~Widget()
{
    delete ui;
}

输出如下所示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值