QT Development (5) - Project combat: use of stopwatch, QTime, Qtimer

Let's continue our QT journey. This time, what we wrote is still a small thing, that is, a stopwatch. This stopwatch is mainly a logical operation. What is it like

write picture description here

It's very simple here. Let's analyze the controls first. The top is a QLCDNumber to display the seconds, and the four buttons in the middle are used for control. The following is the logic of recording time points. The control uses QTextBrowser. Okay, we Take a look at the header file

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTime>
#include <QTimer>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public:
    void init();

private slots:

    void updateDisplay();

    void on_btn_start_clicked();

    void on_btn_stop_clicked();

    void on_btn_pause_clicked();

    void on_btn_point_clicked();

private:
    Ui::MainWindow *ui;

    QTimer *pTimer;
    QTime baseTime;
    //显示的时间
    QString timeStr;
};

#endif // MAINWINDOW_H

As you can see, I defined a public function init to initialize some data, five slot functions, four buttons and a countdown slot function, and defined some variables, of which timeStr is the final display on the LCD. Text, ok, let's start the analysis

//初始化
void MainWindow::init()
{
    //默认显示
    this->ui->lcd_play->display("00:00:00:000");

    this->pTimer = new QTimer;
    //绑定定时器的信号
    connect(this->pTimer,SIGNAL(timeout()),this,SLOT(updateDisplay()));
}

The first is that we first define the format to be displayed as 00:00:00:000 in the initialization, because we can only set this through code, and the second is to define a timer whose signal is timeout

//开始
void MainWindow::on_btn_start_clicked()
{
    //获取当前时间
    this->baseTime = this->baseTime.currentTime();
    this->pTimer->start(1);

    //重置状态
    if(this->ui->btn_stop->text() != "停止")
    {
        this->ui->btn_stop->setText("停止");
    }
    if(this->ui->btn_pause->text() != "暂停")
    {
        this->ui->btn_pause->setText("暂停");
    }
}

Then after we click the start slot function, we get the current time and set it on baseTime, and reset the state of other buttons

//结束
void MainWindow::on_btn_stop_clicked()
{
    if(this->ui->btn_stop->text() == "停止")
    {
        this->ui->btn_stop->setText("清空");
        this->pTimer->stop();
    }
    else if(this->ui->btn_stop->text() == "清空")
    {
        this->ui->btn_stop->setText("停止");
        this->ui->tb_display->clear();
        this->ui->lcd_play->display("00:00:00:000");
    }

}

We have two states at the end. When we stop, we directly call the stop of the timer pointer. If we clear it, we will restore the next state.

//暂停
void MainWindow::on_btn_pause_clicked()
{
    static QTime pauseTime;
    if(this->ui->btn_pause->text() == "暂停")
    {
        pauseTime = QTime::currentTime();
        this->ui->btn_pause->setText("继续");
        this->pTimer->stop();
    }
    else if(this->ui->btn_pause->text() == "继续")
    {
        //进行差值计算
        QTime cut = QTime::currentTime();
        int t = pauseTime.msecsTo(cut);
        this->baseTime = this->baseTime.addMSecs(t);
        this->ui->btn_pause->setText("暂停");
        this->pTimer->start();
    }
}

We are pausing here. First, we define a pauseTime to get the time you clicked, and then call stop. If you press continue again, I will compare the pauseTime I saved with the previous baseTime and add it to make up for it. This time difference, and finally call the start function

//打点
void MainWindow::on_btn_point_clicked()
{
    //将timeStr添加到列表
    this->ui->tb_display->append(this->timeStr );
}

It is relatively simple to do it, just add it directly

The update time in the corresponding slot function

//更新时间
void MainWindow::updateDisplay()
{
    /*
     * 1.点击开始后获取到当前的时间并且赋值给baseTime
     * 2.启动定时器,间隔1s
     * 3.槽函数中再次获取当前的时间currTime
     * 4.计算两个时间的差值t
     * 5.声明一个showTime对象给他加上t的差值
     * 6.格式化后设置显示
     */
    QTime currTime = QTime::currentTime();
    int t = this->baseTime.msecsTo(currTime);
    QTime showTime(0,0,0,0);
    showTime = showTime.addMSecs(t);
    this->timeStr = showTime.toString("hh:mm:ss:zzz");
    this->ui->lcd_play->display(timeStr);
}

It is also the same way of calculating the difference to make up

Okay, let's look at all the code

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->init();
}

MainWindow::~MainWindow()
{
    delete this->pTimer;
    delete ui;
}
//初始化
void MainWindow::init()
{
    //默认显示
    this->ui->lcd_play->display("00:00:00:000");

    this->pTimer = new QTimer;
    //绑定定时器的信号
    connect(this->pTimer,SIGNAL(timeout()),this,SLOT(updateDisplay()));
}

//开始
void MainWindow::on_btn_start_clicked()
{
    //获取当前时间
    this->baseTime = this->baseTime.currentTime();
    this->pTimer->start(1);

    //重置状态
    if(this->ui->btn_stop->text() != "停止")
    {
        this->ui->btn_stop->setText("停止");
    }
    if(this->ui->btn_pause->text() != "暂停")
    {
        this->ui->btn_pause->setText("暂停");
    }
}
//结束
void MainWindow::on_btn_stop_clicked()
{
    if(this->ui->btn_stop->text() == "停止")
    {
        this->ui->btn_stop->setText("清空");
        this->pTimer->stop();
    }
    else if(this->ui->btn_stop->text() == "清空")
    {
        this->ui->btn_stop->setText("停止");
        this->ui->tb_display->clear();
        this->ui->lcd_play->display("00:00:00:000");
    }

}
//暂停
void MainWindow::on_btn_pause_clicked()
{
    static QTime pauseTime;
    if(this->ui->btn_pause->text() == "暂停")
    {
        pauseTime = QTime::currentTime();
        this->ui->btn_pause->setText("继续");
        this->pTimer->stop();
    }
    else if(this->ui->btn_pause->text() == "继续")
    {
        //进行差值计算
        QTime cut = QTime::currentTime();
        int t = pauseTime.msecsTo(cut);
        this->baseTime = this->baseTime.addMSecs(t);
        this->ui->btn_pause->setText("暂停");
        this->pTimer->start();
    }
}
//打点
void MainWindow::on_btn_point_clicked()
{
    //将timeStr添加到列表
    this->ui->tb_display->append(this->timeStr);
}
//更新时间
void MainWindow::updateDisplay()
{
    /*
     * 1.点击开始后获取到当前的时间并且赋值给baseTime
     * 2.启动定时器,间隔1s
     * 3.槽函数中再次获取当前的时间currTime
     * 4.计算两个时间的差值t
     * 5.声明一个showTime对象给他加上t的差值
     * 6.格式化后设置显示
     */
    QTime currTime = QTime::currentTime();
    int t = this->baseTime.msecsTo(currTime);
    QTime showTime(0,0,0,0);
    showTime = showTime.addMSecs(t);
    this->timeStr = showTime.toString("hh:mm:ss:zzz");
    this->ui->lcd_play->display(timeStr);
}

Let's look at the effect

write picture description here

Ok, finally send the source code: click to download

Interested can join the group: 690351511

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325612667&siteId=291194637