一、想法以及结果
使用qt5编写一个小程序记录每天到工位的时间,以及学习or摸鱼的时间。程序基本构思为有两组计时器,第一组计时器在启动软件后(本来是想将打包后的程序设置成开机自启动的,但暂时还没弄,后续学习后可能加上)就开始计时并且不能打断,第二组计时器有开始和暂停按钮,可以作为学习or摸鱼时间记录。
同时为了避免关闭软件丢失记录的数据,程序可以在关闭时自动保存数据,也可以手动保存数据,再次打开程序可以读取数据并继续计时(仅限当天的数据,保证一天的数据连续性)。
UI界面如下:

二、代码实现
创建项目可以使用QMainWindow和QWidget均可,pro文件和main文件均为qt自动生成没有修改。
mainwindows.h文件如下。使用的重写void timerEvent(QTimerEvent *event)来操作定时器,方便使用id区分定时器。
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTime>
#include <QTimer>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_btn_start_clicked();
void on_btn_pause_clicked();
void on_btn_save_clicked();
void timerEvent(QTimerEvent *event);
private:
Ui::MainWindow *ui;
int Timer_all;
int Timer_play;//定时器ID
QTime Time_play;
QTime Time_all;
//显示的时间
QString timeStr_all;
QString timeStr_play;
//读取文件
QStringList lastStr;
};
#endif // MAINWINDOW_H
mainwindows.cpp文件如下。设置程序开始即最小化,不影响其他活动;程序开始便读取保存在根目录下的time.txt文件,如果有今天的数据便从今天的数据继续计时,没有就重新开始。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QTimer>
#include<QDebug>
#include<QFile>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//设置图标
setWindowIcon(QIcon(":/new/10001.png"));
showMinimized();//窗口最小化
//读取文件
QFile f( "./time.txt");
if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << ("打开文件失败");
}
QTextStream txtInput(&f);
QString lineStr;
while(!txtInput.atEnd())
{
lineStr = txtInput.readLine();
}
lastStr=lineStr.split(',');
//判断最后一行的时间是否是今天,用于断点继续
if(lastStr[0]==QDateTime::currentDateTime().toString("yyyy-MM-dd"))
{
ui->lcdNumber_all->display(lastStr[1]);
//QString转为QTime类型
Time_all=QTime::fromString(lastStr[1], "hh:mm:ss");
if(lastStr[2]=="")
{
Time_play.setHMS(0,0,0);
ui->lcdNumber_play->display("00:00:00");
}
else
{
Time_play=QTime::fromString(lastStr[2], "hh:mm:ss");
ui->lcdNumber_play->display(lastStr[2]);
timeStr_play=lastStr[2];
}
}
else
{
Time_play.setHMS(0,0,0);
Time_all.setHMS(0,0,0);
//默认显示
ui->lcdNumber_all->display("00:00:00");
ui->lcdNumber_play->display("00:00:00");
}
f.close();
Timer_all=startTimer(1000);
}
MainWindow::~MainWindow()
{
killTimer(Timer_all);
this->on_btn_save_clicked();//关闭程序自动保存
delete ui;
}
void MainWindow::on_btn_start_clicked()
{
Timer_play=startTimer(1000);
}
void MainWindow::on_btn_pause_clicked()
{
killTimer(Timer_play);
}
void MainWindow::timerEvent(QTimerEvent *event)
{
if(event->timerId()==Timer_play)
{
Time_play=Time_play.addSecs(1);
this->timeStr_play = Time_play.toString("hh:mm:ss");
this->ui->lcdNumber_play->display(timeStr_play);
}
if(event->timerId()==Timer_all)
{
Time_all=Time_all.addSecs(1);
this->timeStr_all = Time_all.toString("hh:mm:ss");
this->ui->lcdNumber_all->display(timeStr_all);
}
}
void MainWindow::on_btn_save_clicked()
{
QDateTime dateTime= QDateTime::currentDateTime();//获取系统当前的时间
QString str = dateTime.toString("yyyy-MM-dd");//格式化时间
//对文件进行操作
QFile file( "./time.txt");
if(file.open(QIODevice::Append)){
QString temp;// 记录写入内容
QTextStream out(&file);
//写入的数据依次是当前时间,总时间,游玩时间,总时间-游玩时间(以小时计算并取整)
temp=str+','+timeStr_all+','+timeStr_play+','+
QString::number(Time_play.secsTo(Time_all)/3600)+'\n';
// 将内容写入文件
out << temp;
file.close();
// qDebug()<<u8"保存文件成功!";
}else{
// qDebug()<<u8"保存文件失败!";
}
}
三、遇到的小问题及解决方法
1、程序打包方法
借鉴:
QT 软件打包为一个单独可执行.exe文件流程_qt打包成可执行程序_Ethan_LiuQuan的博客-CSDN博客
我是用的方法一:打开命令行工具cmd,进入windeployqt.exe目录再进入新建的文件夹目录,使用 windeployqt 对生成的exe 文件进行打配置动态库文件。但因为我之前电脑装的有Anaconda3,里面有pyQt等会冲突报错,可以在环境变量中将qt的windeployqt.exe所在目录(一般是D:\***\Qt\5.15.2\msvc2019_64\bin,msvc2019_64这一级要根据自己用的编译器来选,可能是mingw、msvc2019等)上移到Anaconda3之前,如图

2、中文乱码
可以在双引号之前加上u8,如 qDebug()<<u8"保存文件成功!";
本文描述了一个使用Qt5开发的小程序,用于记录每天到达工位和学习/摸鱼的时间。程序包含两个计时器,一个用于连续计时,另一个用于暂停/开始。数据在关闭时自动保存,支持读取上次的记录。文中还提及了打包程序和解决中文乱码的问题。


被折叠的 条评论
为什么被折叠?



