Qt events & timers

table of Contents

1. Mouse events

1. Need to know what mouse events are

2. Need to know the mouse event processing function of the QLabel control

3. Write the mouse event processing function of the QLabel control

2. Timer implementation method

1. Timer event

2. Timer class QTimer

3. Event dispatcher

4. Event event filter


1. Mouse events

Take the mouse events of the QLabel control as an example:

1. Need to know what mouse events are

enum QEvent::Type

2. Need to know the mouse event processing function of the QLabel control

3. Write the mouse event processing function of the QLabel control

Get xy coordinates: ev->x() ev->y()

Format string: QString("%1, %2").arg(111).arg(222)

Judge all keys: ev->button(), the key values ​​of the keys are as follows

Important content:

QString str = QString("鼠标按下了,x=%1,y=%2").arg(ev->x()).arg(ev->y());
if (ev->button() == Qt::LeftButton) //左键按下判断
{
    QString str = QString("鼠标按下了,x=%1,y=%2").arg(ev->x()).arg(ev->y());
    qDebug() <<str.toUtf8().data();
}

all content:

#include <QLabel>
#include <QEvent>
#include <QMouseEvent>

class mylabel : public QLabel
{
    Q_OBJECT
public:
    explicit mylabel(QWidget *parent = 0);

    virtual void enterEvent(QEvent *event);//鼠标进入事件 虚函数重写
    virtual void leaveEvent(QEvent *event);//鼠标离开事件 虚函数重写
    virtual void mouseMoveEvent(QMouseEvent * ev);//鼠标移动事件 虚函数重写
    virtual void mousePressEvent(QMouseEvent * ev);//鼠标按下事件 虚函数重写
    virtual void mouseReleaseEvent(QMouseEvent * ev);//鼠标释放事件 虚函数重写

signals:

public slots:
};
#include "mylabel.h"
#include "QDebug"
#include "QString"

mylabel::mylabel(QWidget *parent) : QLabel(parent)
{}
//鼠标进入事件
void mylabel::enterEvent(QEvent *event)
{qDebug() <<"鼠标进入";}

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

//鼠标移动事件 虚函数重写
void mylabel::mouseMoveEvent(QMouseEvent * ev)
{qDebug() <<"鼠标移动";}

//鼠标按下事件 虚函数重写
void mylabel::mousePressEvent(QMouseEvent * ev)
{
    if (ev->button() == Qt::LeftButton) {//左键按下判断
        //格式化字符串
        QString str = QString("鼠标按下了,x=%1,y=%2").arg(ev->x()).arg(ev->y());
        qDebug() <<str.toUtf8().data();}
}
//鼠标释放事件 虚函数重写
void mylabel::mouseReleaseEvent(QMouseEvent * ev)
{qDebug() <<"鼠标释放";}

Second, the timer implementation method

Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{
    ui->setupUi(this);
    //定时器的第一种方法:通过定时器事件和重写定时器处理函数
    //启动定时器,周期单位ms
    //timer1_id = startTimer(1000);
    //timer2_id = startTimer(10000);

    //定时器的第二种方法:通过定时器类
    QTimer *timer = new QTimer(this);
    timer->start(500);
    connect(timer, &QTimer::timeout, [=](){
        static int num = 1;
        ui->label->setNum(num++);
    });
    //通过点击按钮停止定时器
    connect(ui->btn_stop, &QPushButton::clicked, [=](){
        if (timer->isActive())
            timer->stop();
        else
            timer->start(500);
    });
}

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

//定时器的第一种方法:重写定时器事件处理函数
void Widget::timerEvent(QTimerEvent *ev)
{
    static int num = 1;

    if (ev->timerId() == timer1_id){
        ui->label->setNum(num++);
    } else if (ev->timerId() == timer2_id){
        num = 1;
    }
}

1. Timer event

1. QEvent::Timer event

2. The processing function of QEvent::Timer event is void timerEvent(QTimerEvent *ev)

3. Start the timer startTimer(1000) milliseconds, and return the unique ID of the timer

4. When there are multiple timers, you can compare the timer ID returned by startTimer() with the timer ID in the QTimerEvent event obtained through QTimerEvent->timerid(). This ID is the timer ID returned by startTimer()

2. Timer class QTimer

1. Create a timer QTimer *timer = new QTimer(this);

2. Start the timer timer->start(500);

3. Bind signals and slots through timeout signals

Three, event event dispatcher

bool QObject::event(QEvent *e) event dispatcher, you can override this virtual function in the QObject derived class to customize the event distribution. This is not recommended in actual development! ! https://www.cnblogs.com/zhaobinyouth/p/7688954.html

 //重写event事件分发器,拦截鼠标按下事件,实际工程并不会拦截!!!!!
 bool mylabel::event(QEvent *e)
 {
    //自己处理按钮 按下事件
    if (e->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *ev = dynamic_cast<QMouseEvent *>(e);//类型转换,将父类对象转为子类对象
        QString str = QString("Event函数中,鼠标按下了,x=%1,y=%2").arg(ev->x()).arg(ev->y());
        qDebug() <<str.toUtf8().data();

        return true;//表示该事件已经被处理了,true代表用户自己处理这个事件 不向下分发
    }

    //其他事件交给父类处理,默认处理
    return QLabel::event(e);
 }

Four, event event filter

Before the program distributes the event to the event distributor, you can use filters to intercept

 

Guess you like

Origin blog.csdn.net/m0_37845735/article/details/108439133
Recommended