【Qt】7.定时器第一种方式、定时器第二种方式、event事件、事件过滤器

目录

定时器第一种方式

代码

widget.h

widget.cpp

结果

定时器第二种方式

代码

widget.cpp

结果

event事件

代码

mylabel.h

mylabel.cpp

结果

事件过滤器

代码

widget.h

widget.cpp

结果

定时器第一种方式

  1. timerEvent事件

  2. 启动定的器startTimer(豪秒),返回值是id号

  3. 区分定时器timerld

代码

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();

private:
    Ui::Widget *ui;

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

    //定时器标识号
    int id1,id2;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QTimerEvent>

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

    //启动定时器
    id1=startTimer(1000);//每1000ms调用一次
    id2=startTimer(2000);
}

//定时器事件
void Widget::timerEvent(QTimerEvent *e){
    if(e->timerId()==id1){
        static int num=0;
        ui->label_2->setText(QString::number(num++));
    }

    if(e->timerId()==id2){
        static int num2=0;
        ui->label_3->setText(QString::number(num2++));
    }
}

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

结果

定时器第二种方式

  1. QTimer头文件

  2. 创建 QTimer *timer

  3. 启动定时器timer->start(毫秒)

  4. 发送信号timeout

  5. 暂停stop

代码

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QTimerEvent>
#include <QTimer>

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

    //启动定时器
    id1=startTimer(1000);//每1000ms调用一次
    id2=startTimer(2000);

    //定时器第2种方式
    QTimer *timer1=new QTimer(this);
    //启动定时器对象
    timer1->start(500);//ms
    //每隔0.5秒发送信号
    connect(timer1,&QTimer::timeout,[=](){
        static int num=0;
        ui->label_4->setText(QString::number(num++));
    });

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

//定时器事件
void Widget::timerEvent(QTimerEvent *e){
    if(e->timerId()==id1){
        static int num=0;
        ui->label_2->setText(QString::number(num++));
    }

    if(e->timerId()==id2){
        static int num2=0;
        ui->label_3->setText(QString::number(num2++));
    }
}

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

结果

event事件

  1. 主要功能:事件的分发

  2. bool event (QEvent *e)

  3. 返回值如果是true代表用户自己处理

  4. false系统处理最好抛给父类去处理

  5. static.cast<转换类型>(原对象)

  6. e->type 具体事件

代码

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 *);

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

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

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

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

    //事件分发,Event事件
    bool event(QEvent *e);

signals:

};

#endif // MYLABEL_H

mylabel.cpp

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

MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
{
    //设置鼠标追踪
    this->setMouseTracking(true);
}

//鼠标进入
void MyLabel::enterEvent(QEvent *){
    qDebug()<<"鼠标进入";
}

//鼠标离开
void MyLabel::leaveEvent(QEvent *){
    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;
    }
}

//鼠标释放
void MyLabel::mouseReleaseEvent(QMouseEvent *ev){
    //如果鼠标按下的是左键,然后提示内容
    if(ev->button()==Qt::LeftButton){
        QString str=QString("鼠标释放 x=%1 y=%2").arg(ev->x()).arg(ev->y());
        qDebug()<<str;
    }
}

//鼠标移动
void MyLabel::mouseMoveEvent(QMouseEvent *ev){
    //如果鼠标按下的是左键,然后提示内容,持续状态需要用buttons,用与操作符进行判断
    //if(ev->buttons()&Qt::LeftButton){
        QString str=QString("鼠标移动 x=%1 y=%2").arg(ev->x()).arg(ev->y());
        qDebug()<<str;
    //}
}

//事件分发,Event事件
bool MyLabel::event(QEvent *e){
    //通常不会做拦截,event只做分发事件
    if(e->type()==QEvent::MouseButtonPress){
        QMouseEvent *ev=static_cast<QMouseEvent *>(e);
        QString str=QString("Event,鼠标按下 x=%1 y=%2").arg(ev->x()).arg(ev->y());
        qDebug()<<str;

        //只有鼠标按下,自己处理,返回true
        return true;
    }

    //其他事件,让父亲做默认处理
    return QLabel::event(e);
}

结果

事件过滤器

  1. 哪个控件需求过滤事件就给哪个控件安装过滤器

  2. 步骤1安装过滤器

  3. ui->label->installEventFilter(this);

  4. 步骤2重写事件

代码

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();

private:
    Ui::Widget *ui;

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

    //定时器标识号
    int id1,id2;

    //事件过滤器的事件
    bool eventFilter(QObject *,QEvent *);
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QTimerEvent>
#include <QTimer>
#include <QMouseEvent>
#include <QDebug>

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

    //启动定时器
    id1=startTimer(1000);//每1000ms调用一次
    id2=startTimer(2000);

    //定时器第2种方式
    QTimer *timer1=new QTimer(this);
    //启动定时器对象
    timer1->start(500);//ms
    //每隔0.5秒发送信号
    connect(timer1,&QTimer::timeout,[=](){
        static int num=0;
        ui->label_4->setText(QString::number(num++));
    });

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

    //给ui->label做事件过滤器拦截
    //步骤1 给控件安装过滤器
    //参数this,通过父窗口给lable安装过滤器
    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);
            QString str=QString("事件过滤器,鼠标按下 x=%1 y=%2").arg(ev->x()).arg(ev->y());
            qDebug()<<str;

            //只有鼠标按下,自己处理,返回true
            return true;
        }
    }

    //其他让父类处理
    return QWidget::eventFilter(obj,e);
}

//定时器事件
void Widget::timerEvent(QTimerEvent *e){
    if(e->timerId()==id1){
        static int num=0;
        ui->label_2->setText(QString::number(num++));
    }

    if(e->timerId()==id2){
        static int num2=0;
        ui->label_3->setText(QString::number(num2++));
    }
}

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

结果

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现定时器页面,可以按照以下步骤: 1. 创建一个新的Qt Widgets应用程序工程。 2. 在主窗口中添加一个QLabel控件用于显示计时器的值。 3. 创建一个新的类用于实现定时器功能,例如名为"TimerPage"的类。 4. 在TimerPage类中实现两种方式来实现定时器方式1:重写定时器事件TimerPage类中重写定时器事件函数timerEvent(QTimerEvent *event),并在该函数中更新计时器的值并更新QLabel控件的显示。 ``` class TimerPage : public QObject { Q_OBJECT public: TimerPage(QObject *parent = nullptr) : QObject(parent) { //启动定时器 startTimer(1000); } protected: void timerEvent(QTimerEvent *event) override { //更新计时器的值 m_time++; //更新QLabel控件的显示 m_label->setText(QString("Time: %1s").arg(m_time)); } private: int m_time = 0; QLabel *m_label = new QLabel(); }; ``` 方式2:使用QTimer类 在TimerPage类中创建一个QTimer对象,并在其构造函数中连接timeout()信号到一个槽函数中,在该槽函数中更新计时器的值并更新QLabel控件的显示。 ``` class TimerPage : public QObject { Q_OBJECT public: TimerPage(QObject *parent = nullptr) : QObject(parent) { //创建QTimer对象 m_timer = new QTimer(this); //连接timeout()信号到槽函数onTimeout() connect(m_timer, &QTimer::timeout, this, &TimerPage::onTimeout); //启动定时器 m_timer->start(1000); } private slots: void onTimeout() { //更新计时器的值 m_time++; //更新QLabel控件的显示 m_label->setText(QString("Time: %1s").arg(m_time)); } private: int m_time = 0; QLabel *m_label = new QLabel(); QTimer *m_timer = nullptr; }; ``` 5. 在主窗口中实例化TimerPage类,并将其QLabel控件添加到主窗口中。 ``` class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) { //创建TimerPage对象 TimerPage *timerPage = new TimerPage(this); //将QLabel控件添加到主窗口中 setCentralWidget(timerPage->label()); } }; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

因心,三人水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值