最近项目里需要一个计时功能,可以实现暂停,继续,结束,开始的功能,如同秒表一样,我就写了一个demo,效果如图:
代码如下:
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTimer>
#include <QLCDNumber>
#include <QDateTime>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>class Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = 0);~Widget();
private:void InitWidget();void InitConnect();
private:void slotPushButtonStart();void slotPushButtonPause();void slotPushButtonContinue();void slotPushButtonStop();
private:QTimer* m_timer=nullptr;QLCDNumber* m_displaytime=nullptr;QDateTime m_startclock, m_pausetime;qint64 m_pausedTime;QPushButton* PushButtonStart=nullptr;QPushButton* PushButtonPause=nullptr;QPushButton* PushButtonContinue=nullptr;QPushButton* PushButtonStop=nullptr;
};#endif // WIDGET_H
#include "widget.h"
#include <QDebug>Widget::Widget(QWidget *parent): QWidget(parent)
{InitWidget();InitConnect();
}Widget::~Widget()
{
}void Widget::InitWidget()
{QVBoxLayout* vlayout=new QVBoxLayout(this);QHBoxLayout* hlayout=new QHBoxLayout(this);m_displaytime = new QLCDNumber(this);m_displaytime->setDigitCount(8);m_displaytime->setMode(QLCDNumber::Dec);m_displaytime->resize(500, 100);m_displaytime->setSegmentStyle(QLCDNumber::Flat);m_displaytime->setStyleSheet("background:transparent;color:#000000;");m_displaytime->move(QPoint(28, 763));m_displaytime->display("00:00:00");PushButtonStart=new QPushButton(this);PushButtonPause=new QPushButton(this);PushButtonContinue=new QPushButton(this);PushButtonStop=new QPushButton(this);PushButtonStart->setText("开始");PushButtonStart->setFixedSize(150,80);PushButtonPause->setText("暂停");PushButtonPause->setFixedSize(150,80);PushButtonContinue->setText("继续");PushButtonContinue->setFixedSize(150,80);PushButtonStop->setText("结束");PushButtonStop->setFixedSize(150,80);vlayout->addWidget(m_displaytime);vlayout->addStretch();vlayout->addWidget(PushButtonStart);vlayout->addStretch();vlayout->addWidget(PushButtonPause);vlayout->addStretch();vlayout->addWidget(PushButtonContinue);vlayout->addStretch();vlayout->addWidget(PushButtonStop);vlayout->addStretch();this->setLayout(vlayout);m_timer = new QTimer(this);m_timer->setInterval(1000);connect(m_timer, &QTimer::timeout, [=] {QDateTime currentDateTime = QDateTime::currentDateTime();qint64 ms = m_startclock.msecsTo(currentDateTime);qint64 seconds = ms / 1000;qint64 minutes = seconds / 60;qint64 hours = minutes / 60;// 更新分钟和秒数,以处理进位minutes %= 60;seconds %= 60;// 格式化显示时间m_displaytime->display(QString("%1:%2:%3").arg(hours, 2, 10, QLatin1Char('0')).arg(minutes, 2, 10, QLatin1Char('0')).arg(seconds, 2, 10, QLatin1Char('0')));});setFixedSize(160,500);PushButtonContinue->setEnabled(false);
}void Widget::InitConnect()
{connect(PushButtonStart,&QPushButton::clicked,this,&Widget::slotPushButtonStart);connect(PushButtonPause,&QPushButton::clicked,this,&Widget::slotPushButtonPause);connect(PushButtonContinue,&QPushButton::clicked,this,&Widget::slotPushButtonContinue);connect(PushButtonStop,&QPushButton::clicked,this,&Widget::slotPushButtonStop);
}void Widget::slotPushButtonStart()
{m_timer->start();m_startclock = QDateTime::currentDateTime();m_displaytime->display("00:00:00");PushButtonPause->setEnabled(true);
}void Widget::slotPushButtonPause()
{m_pausedTime = m_startclock.msecsTo(QDateTime::currentDateTime());m_timer->stop();PushButtonContinue->setEnabled(true);PushButtonStart->setEnabled(false);}void Widget::slotPushButtonContinue()
{m_startclock = QDateTime::currentDateTime().addMSecs(-m_pausedTime);m_timer->start();
}void Widget::slotPushButtonStop()
{m_timer->stop();PushButtonPause->setEnabled(false);PushButtonStart->setEnabled(true);PushButtonContinue->setEnabled(false);
}