QT Object定时器使用

本文介绍了如何在Qt中使用QWidget的定时器功能,包括重写timerEvent函数以处理定时器事件,以及startTimer和killTimer方法的使用。
#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);

    // 需要对timerEvent函数进行重写
    virtual void timerEvent(QTimerEvent* event);

    ~Widget();

private slots:
    void on_startTimer_clicked();



    void on_killTimer_clicked();

private:
    Ui::Widget *ui;
    int eventId; // 定时器返回的唯一id,用于区分不同定时器
    int pictureId=1; // 不同图片的编号
};
#endif // WIDGET_H

#include "widget.h"
#include "ui_widget.h"

#define TIME_OUT 1 * 1000

/**
QObject中有定时器,可以直接进行使用startTimer和killTimer操作
但需要对timerEvent进行重新定义,以自己的方式处理定时器
*/
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    QString path("D:\\workspace\\qt\\Timer\\");
    path += QString::number(this->pictureId)+".jpg";

    QPixmap pix(path);
    ui->label->setPixmap(pix);
}

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


void Widget::on_startTimer_clicked()
{
    // 开启定时器
    // QObject中有定时器,直接使用,Widget->QWidget->QObject
    this->eventId = this->startTimer(TIME_OUT);

}


void Widget::timerEvent(QTimerEvent* event) {

    // 获取timerId是否为开启定时器所操作的
    if (event->timerId() == this->eventId) {
        QString path("D:\\workspace\\qt\\Timer\\");
        path += QString::number(this->pictureId)+".jpg";

        QPixmap pix(path);
        ui->label->setPixmap(pix);
		this->pictureId++;

        if (this->pictureId == 5) {
            this->pictureId = 1;
        }
    }
}

void Widget::on_killTimer_clicked()
{
    this->killTimer(this->eventId);
}


Qt 中,定时器使用主要有三种方式:`QTimer` 类、`QObject::startTimer()` 方法以及 `QTimer::singleShot` 静态方法。以下是具体的使用方法及示例代码: ### 1. 使用 `QTimer` 类 `QTimer` 是 Qt 提供的一个高级定时器类,适合大多数 GUI 应用程序的定时任务。可以用于周期性地执行某个操作。 #### 示例代码:定时器每隔 1 秒切换标签上的图片 ```cpp #include <QApplication> #include <QLabel> #include <QTimer> #include <QPixmap> int main(int argc, char *argv[]) { QApplication app(argc, argv); QLabel label; QPixmap pixmap1(":/image1.png"); QPixmap pixmap2(":/image2.png"); // 初始化显示第一张图片 label.setPixmap(pixmap1); label.show(); // 创建定时器 QTimer timer; QObject::connect(&timer, &QTimer::timeout, [&]() { static bool isPixmap1 = true; if (isPixmap1) { label.setPixmap(pixmap2); } else { label.setPixmap(pixmap1); } isPixmap1 = !isPixmap1; }); // 启动定时器,每1秒触发一次 timer.start(1000); return app.exec(); } ``` 现象:每隔 1 秒,标签上的图片会在 `image1.png` 和 `image2.png` 之间切换 [^1]。 ### 2. 使用 `QTimer::singleShot` 如果只需要定时执行一次操作,可以使用 `QTimer::singleShot` 方法。该方法在指定时间后执行一次回调函数。 #### 示例代码:单次定时器 ```cpp #include <QCoreApplication> #include <QTimer> #include <QObject> #include <QDebug> class MyObject : public QObject { Q_OBJECT public slots: void onTimeout() { qDebug() << "Single shot timeout occurred!"; } }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MyObject myObject; // 设置单次定时器,1秒后触发 onTimeout 槽函数 QTimer::singleShot(1000, &myObject, &MyObject::onTimeout); return a.exec(); } ``` 现象:1 秒后输出 `Single shot timeout occurred!` [^2]。 ### 3. 使用 `QObject::startTimer()` 和 `timerEvent()` 对于需要创建多个定时器的场景,可以通过 `QObject::startTimer()` 启动定时器,并在 `timerEvent()` 中处理定时器事件。 #### 示例代码:创建多个定时器 ```cpp #include <QObject> #include <QTimerEvent> #include <QDebug> class MyObject : public QObject { Q_OBJECT private: QList<int> timerIdList; public: MyObject(QObject *parent = nullptr) : QObject(parent) { // 创建100个定时器,每个定时器间隔为1秒 for (int i = 0; i < 100; ++i) { timerIdList.append(startTimer(1000)); } } protected: void timerEvent(QTimerEvent *event) override { for (int i = 0; i < timerIdList.size(); ++i) { if (timerIdList.at(i) == event->timerId()) { qDebug() << "Timer" << i + 1 << "triggered!"; break; } } } }; ``` 现象:每隔 1 秒,所有定时器都会触发一次,输出对应的定时器编号 [^4]。 ### 4. 使用多个 `QTimer` 对象 在需要多个定时器的情况下,可以直接创建多个 `QTimer` 实例,并分别连接到不同的槽函数。 #### 示例代码:多个定时器 ```cpp #include <QApplication> #include <QLabel> #include <QTimer> #include <QDateTime> int main(int argc, char *argv[]) { QApplication app(argc, argv); QLabel label1("Timer 1: "); QLabel label2("Timer 2: "); label1.show(); label2.show(); QTimer timer1; QTimer timer2; QObject::connect(&timer1, &QTimer::timeout, [&]() { label1.setText("Timer 1: " + QDateTime::currentDateTime().toString()); }); QObject::connect(&timer2, &QTimer::timeout, [&]() { label2.setText("Timer 2: " + QDateTime::currentDateTime().toString()); }); // 启动两个定时器,分别间隔1秒和2秒 timer1.start(1000); timer2.start(2000); return app.exec(); } ``` 现象:第一个定时器每隔 1 秒更新一次标签内容,第二个定时器每隔 2 秒更新一次标签内容 [^5]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值