更多完整QT视频资料、文档视频、学习路线图等领取q君羊:487707667
本文主要总结一个自定义的计时器(不是定时器),该计时器每个一秒钟刷新一次。博主封装一个函数timeCounter(),获取每一秒的时分秒数值,封装函数返回值是一个QStringList值,里面依次返回时、分、秒三个值;封装函数代码如下:
QStringList Widget::timeCounter()
{
/*最多计算:24天=2073600000ms,也就是576小时*/
int elapsed = time->elapsed()/1000;
short int hour = 0;
short int minute = 0;
short int second = 0;
if(elapsed>=3600)
{
/*n hour*/
hour=elapsed/3600;
elapsed =elapsed%3600;
}
else if(elapsed>=60)
{
/*n minute*/
minute=elapsed/60;
second =elapsed%60;
}
else if(elapsed<60)
{
/*n second*/
second =elapsed;
}
return QStringList()<<tr("%1").arg(hour)<<tr("%1").arg(minute)<<tr("%1").arg(second);
}
具体的工程示例如下所述。
1.1新建一个widget工程,不要勾选ui界面。然后分别在widget.h、widget.cpp、main.cpp分别添加如下代码。
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QStringList>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
QStringList timeCounter();
private:
void setupUI();
QTime *time;
QTimer *timer;
private slots:
void slot_startedTimer_clicked(bool);
void slot_stopedTimer_clicked(bool);
void slot_timer_timeout();
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QDateTime>
#include <QTime>
#include <QDate>
#include <QTimer>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
setupUI();
}
Widget::~Widget()
{
}
void Widget::setupUI()
{
QVBoxLayout* mainLayout = new QVBoxLayout;
mainLayout->setMargin(0);
mainLayout->setSpacing(0);
QPushButton *startedTimer = new QPushButton(tr("计时器start"));
connect(startedTimer,SIGNAL(clicked(bool)),this,SLOT(slot_startedTimer_clicked(bool)));
QPushButton *stopedTimer = new QPushButton(tr("计时器stop"));
connect(stopedTimer,SIGNAL(clicked(bool)),this,SLOT(slot_stopedTimer_clicked(bool)));
mainLayout->addWidget(startedTimer);
mainLayout->addWidget(stopedTimer);
mainLayout->addStretch();
this->setLayout(mainLayout);
/*初始化时间*/
time = new QTime();
timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(slot_timer_timeout()));
}
void Widget::slot_startedTimer_clicked(bool)
{
qDebug()<<tr("开启计时器");
time->start();
timer->start(1000);
}
void Widget::slot_stopedTimer_clicked(bool)
{
qDebug()<<tr("停止计时器");
timer->stop();
}
void Widget::slot_timer_timeout()
{
qDebug()<<timeCounter();
}
QStringList Widget::timeCounter()
{
/*最多计算:24天=2073600000ms,也就是576小时*/
int elapsed = time->elapsed()/1000;
short int hour = 0;
short int minute = 0;
short int second = 0;
if(elapsed>=3600)
{
/*n hour*/
hour=elapsed/3600;
elapsed =elapsed%3600;
}
else if(elapsed>=60)
{
/*n minute*/
minute=elapsed/60;
second =elapsed%60;
}
else if(elapsed<60)
{
/*n second*/
second =elapsed;
}
return QStringList()<<tr("%1").arg(hour)<<tr("%1").arg(minute)<<tr("%1").arg(second);
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.resize(960,640);
w.show();
return a.exec();
}
1.2程序构建和运行结果如下图所示:
更多完整QT视频资料、文档视频、学习路线图等领取q君羊:487707667
原文链接:https://blog.51cto.com/xiaohaiwa/5380432