Using Qt to design stopwatch function

The example in this article shares the specific code of the QT design stopwatch function for your reference. The specific content is as follows

1. Window Interface Settings

You can use the property bar digitCount to design the initial position of 0 at this time

 

2. Code writing

1. Determine the time -> call an update function every second (for conversion) or directly call the time class in QT; 2. Determine the
display method;
3. Call the display at intervals to achieve dynamic effects;
4. Here You should check the help manual of Qtime/Qlcd
5. When declaring the timer, it should be declared as a pointer, and the signaler of the subsequent slot function is required to be a pointer type;
6. Use the difference between the reference time and the current time to display the timer ;
7. When setting the pause button, it should be noted that two functions can be set for the button, which are pause and continue to form a cycle, but it should be noted that when we pause and then continue, the period of time is also counted. When it is displayed again, there will be a jump at this time , because when we display the time, we use the difference between the current time and the base time to calculate. The solution is:
get the values ​​before and after the pause, save them, and subtract this part of the time when the calculation starts again.
8. Also pay attention to setting the availability of each button (Enabled)

code show as below:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <Qtimer>
#include <Qtime>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void updateTimeAndDisplay();    //槽函数


    void on_btn_start_clicked();

    void on_btn_stop_clicked();

    void on_btn_pause_clicked();

    void on_btn_log_clicked();

private:
    Ui::MainWindow *ui;
    QTimer *qtimer;                 //声明一个定时器对象,声明成指针对象
    QTime baseTime;                 //声明一个时间对象
    QString showStr;                //直接声明
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->qtimer = new QTimer;   //实例构造
    connect(this->qtimer,SIGNAL(timeout()),this,SLOT(updateTimeAndDisplay()));
}

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

//计算基准时间和当前时间的差值来显示秒表值(currentTime)
void MainWindow::updateTimeAndDisplay()
{
    QTime current = QTime::currentTime();
    int temp = this->baseTime.msecsTo(current);   //计算差值
    QTime showTime(0,0,0,0);
    showTime = showTime.addMSecs(temp);
    showStr = showTime.toString("hh:mm:ss:zzz");  //时间转换为字符串
    this->ui->lcdNumber->display(showStr);
}
void MainWindow::on_btn_start_clicked()
{
    this->baseTime = QTime::currentTime();
    this->qtimer->start(1);
    this->ui->btn_start->setEnabled(false);    //设置按键的可用性

}

void MainWindow::on_btn_stop_clicked()
{
    if(this->ui->btn_stop->text() == "停止"){
        this->ui->btn_stop->setText("清零");
        this->qtimer->stop();
        this->ui->btn_start->setEnabled(false);
        this->ui->btn_pause->setEnabled(false);

    }else
    {
        this->ui->lcdNumber->display("00:00:00:000");
        this->ui->textBrowser->clear();
        this->ui->btn_stop->setText("停止");
        this->ui->btn_start->setEnabled(true);
        this->ui->btn_pause->setEnabled(true);

    }
}

//获取暂停前后的值求差值
void MainWindow::on_btn_pause_clicked()
{
    static QTime pauseTime;
    if(this->ui->btn_pause->text() == "暂停"){
        pauseTime = QTime::currentTime();
        this->qtimer->stop();
        this->ui->btn_pause->setText("继续");
        this->ui->btn_start->setEnabled(false);
        this->ui->btn_stop->setEnabled(false);
    }else
    {
        QTime cut = QTime::currentTime();
        int t = pauseTime.msecsTo(cut);         //求差值
        this->baseTime = baseTime.addMSecs(t);  //把暂停时间再给baseTime
        this->qtimer->start(1);
        this->ui->btn_pause->setText("暂停");
        this->ui->btn_start->setEnabled(true);
        this->ui->btn_stop->setEnabled(true);

    }

}

void MainWindow::on_btn_log_clicked()
{
    this->ui->textBrowser->append(showStr); //这里如果使用settext()将会覆盖信息,不能追加显示
}

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/131193385