【QT】利用QT做一个简单的LCD的倒计时效果

标签: qt

利用QT做一个简单的LCD的倒计时效果

添加头文件

#include <QMainWindow>
#include <QTimer>
#include <QDateTime>
#include <QDesktopWidget>
#include <QApplication>
#include <QTime>
#include <QCoreApplication>
#include <QEventLoop>
#include <QProcess>
#include <QString>

添加变量函数声明

public:
    QTimer *timer;
    void handle_timeout(void);
    int hour=12,min=0,sec=0;
    void Delay_MSec(unsigned int msec);
    int flag=0;

添加.cpp代码

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
    QDesktopWidget *desktop = QApplication::desktop();
    move((desktop->width()-this->width())/2,(desktop->height()-this->height())/2);

    ui->lcdNumber->setSegmentStyle(QLCDNumber::Flat);
    ui->lcdNumber->setDigitCount(8);

    ui->lcdNumber->display(QDateTime::currentDateTime().toString("12:00:00"));
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,&MainWindow::handle_timeout);
    timer->start(1000);
    flag=1;
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::handle_timeout()
{
    if(flag == 1){
        //定时12个小时
        for(hour = 11;hour>=0;hour--){
        for( min =59;min>=0 ;min--){
            for(sec=59;sec>=0;sec--){
                ui->lcdNumber->display(QDateTime::currentDateTime().toString(QString::number(hour)+":"+ QString::number(min)+":"+QString::number(sec)));

                Delay_MSec(1000);
            }


        }
        }
        timer->stop();


    }

}

void MainWindow::Delay_MSec(unsigned int msec)
{
    QTime _Timer = QTime::currentTime().addMSecs(msec);
    while(QTime::currentTime() < _Timer){
        QCoreApplication::processEvents(QEventLoop::AllEvents,100);
    }
}

演示

 

版权声明:本文为rong11417原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/rong11417/article/details/104573462