【Qt界面开发项目(2)】定时计数器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hua12134/article/details/79863351

本项目主要使用Qt中的两个类,QTime和QTimer。QTime类是一个时间类,显然它是用来获取当前时间;QTimer类是一个定时器类,所谓定时器就是不断的去重复同一件事,首先我们可以查看一下Qt的帮助手册:
这里写图片描述

QTimer类提供了一个循环的并且可以发射信号的类。我们要使用它,就先创建一个QTimer类,连接一个timeout信号和我们自己写的槽函数,并且调用start函数。它将会在指定的时间内(start函数来指定)发射timeout信号。

再说信号与槽机制,可以使用connect方法或者直接转到槽函数。关于connect函数,就是四个参数:connect(谁, 发送什么信号, 谁接收, 作出什么样的反应)

先看看效果图:

这里写图片描述

代码部分(注释都有,不再赘述):

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

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

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void updateTimeAndDisplay();

private slots:
    void on_btn_start_clicked();

    void on_btn_stop_clicked();

    void on_btn_pause_clicked();

    void on_btn_log_clicked();


    void on_actiongf_triggered();

    void on_action_triggered();

private:
    Ui::MainWindow *ui;
    QTimer *ptimer;     //定时器对象指针
    QTime baseTime;     //基准时间
    QString showStr;    //要在LCD中显示的字符串
};

#endif // MAINWINDOW_H

mainwindow.cpp

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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->ptimer = new QTimer;
    connect(this->ptimer, SIGNAL(timeout()), this, SLOT(updateTimeAndDisplay()));

    //设置初始时只能点击开始键,其他三个键不可点击
    this->ui->btn_stop->setEnabled(false);
    this->ui->btn_pause->setEnabled(false);
    this->ui->btn_log->setEnabled(false);
}

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

//槽函数
void MainWindow::updateTimeAndDisplay()
{
    QTime current = QTime::currentTime();           //获取当前时间,取名current
    int t = this->baseTime.msecsTo(current);        //计算从基准时间到当前时间的时间差(毫秒)
    QTime showTime(0,0,0,0);                        //设置显示时间初值
    showTime = showTime.addMSecs(t);                //显示时间为初值加上差值
    showStr = showTime.toString("hh:mm:ss:zzz");    //设置显示时间的格式
    this->ui->lcdNumber->display(showStr);          //LCD按要求显示时间
}

//按下开始键
void MainWindow::on_btn_start_clicked()
{
    this->baseTime = QTime::currentTime();          //获取当前时间,设置为基准时间
    this->ptimer->start(1);                         //设置间隔为1ms
    this->ui->btn_start->setEnabled(false);         //关闭开始键的使能
    this->ui->btn_stop->setEnabled(true);           //同时打开另外三个键的使能
    this->ui->btn_pause->setEnabled(true);
    this->ui->btn_log->setEnabled(true);
}

//按下停止键
void MainWindow::on_btn_stop_clicked()
{
    if (this->ui->btn_stop->text() == "停止")
    {
        this->ptimer->stop();
        this->ui->btn_stop->setText("清零");        
        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->setText("暂停");
        this->ui->btn_log->setEnabled(false);
    }
}

//按下暂停键
void MainWindow::on_btn_pause_clicked()
{
    static QTime pauseTime;                             //暂停时间
    if (this->ui->btn_pause->text() == "暂停")
    {
        this->ui->btn_pause->setText("继续");
        pauseTime = QTime::currentTime();
        this->ui->btn_stop->setEnabled(false);
        this->ptimer->stop();
    }
    else
    {
        QTime now = QTime::currentTime();               //当前时间
        int t = pauseTime.msecsTo(now);                 //时间差值,即暂停的时间
        this->baseTime = this->baseTime.addMSecs(t);    //基准时间变化
        this->ptimer->start(1);
        this->ui->btn_pause->setText("暂停");
        this->ui->btn_stop->setEnabled(true);
    }
}

//按下打点键
void MainWindow::on_btn_log_clicked()
{
    this->ui->textBrowser->append(showStr);
}

void MainWindow::on_actiongf_triggered()
{
    QMessageBox::aboutQt(this, "鸣谢");
}

void MainWindow::on_action_triggered()
{
    QString box = "華仔是一个英俊的有为青年,你同意吗?";
    QMessageBox::question(this, "关于華仔", box, "同意", "非常同意");
}

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

猜你喜欢

转载自blog.csdn.net/hua12134/article/details/79863351