改进很多代码

This commit is contained in:
feiyangqingyun
2021-10-30 19:20:24 +08:00
parent cb138c2ec5
commit f027369f27
104 changed files with 842 additions and 348 deletions

View File

@@ -4,10 +4,13 @@
#include "ui_frmsaveruntime.h"
#include "qfile.h"
#include "saveruntime.h"
#include "qdebug.h"
frmSaveRunTime::frmSaveRunTime(QWidget *parent) : QWidget(parent), ui(new Ui::frmSaveRunTime)
{
ui->setupUi(this);
ui->setupUi(this);
//设置文件存储目录
SaveRunTime::Instance()->setPath(qApp->applicationDirPath() + "/log");
}
frmSaveRunTime::~frmSaveRunTime()
@@ -40,7 +43,12 @@ void frmSaveRunTime::on_btnUpdate_clicked()
void frmSaveRunTime::on_btnOpen_clicked()
{
QString fileName = QString("%1/examples_runtime_%2.txt").arg(qApp->applicationDirPath()).arg(QDate::currentDate().year());
QString path = qApp->applicationDirPath();
QString name = qApp->applicationFilePath();
QStringList list = name.split("/");
name = list.at(list.count() - 1).split(".").at(0);
QString fileName = QString("%1/log/%2_runtime_%3.txt").arg(path).arg(name).arg(QDate::currentDate().year());
QFile file(fileName);
if (file.open(QFile::ReadOnly | QFile::Text)) {
ui->txtMain->setText(file.readAll());

View File

@@ -13,72 +13,92 @@
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QTextEdit" name="txtMain"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string>启动服务</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnAppend">
<property name="minimumSize">
<size>
<width>130</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>插入一条记录</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnUpdate">
<property name="minimumSize">
<size>
<width>130</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>更新一条记录</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnOpen">
<property name="minimumSize">
<size>
<width>130</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>打开记录文件</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
<widget class="QFrame" name="frame">
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>200</width>
<height>16777215</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string>启动运行时间记录</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnAppend">
<property name="minimumSize">
<size>
<width>130</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>插入一条记录</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnUpdate">
<property name="minimumSize">
<size>
<width>130</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>更新一条记录</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnOpen">
<property name="minimumSize">
<size>
<width>130</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>打开记录文件</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</widget>

View File

@@ -24,7 +24,7 @@ int main(int argc, char *argv[])
#endif
frmSaveRunTime w;
w.setWindowTitle("保存运行时间");
w.setWindowTitle("运行时间记录示例 V2022 (QQ: 517216493 WX: feiyangqingyun)");
w.show();
return a.exec();

View File

@@ -1,4 +1,6 @@
#include "saveruntime.h"
#pragma execution_character_set("utf-8")
#include "saveruntime.h"
#include "qmutex.h"
#include "qdir.h"
#include "qfile.h"
@@ -7,12 +9,6 @@
#include "qtextstream.h"
#include "qstringlist.h"
#ifdef Q_OS_WIN
#define NEWLINE "\r\n"
#else
#define NEWLINE "\n"
#endif
QScopedPointer<SaveRunTime> SaveRunTime::self;
SaveRunTime *SaveRunTime::Instance()
{
@@ -72,6 +68,10 @@ void SaveRunTime::getDiffValue(const QDateTime &startTime, const QDateTime &endT
void SaveRunTime::start()
{
if (timerSave->isActive()) {
return;
}
//开始时间变量必须在这,在部分嵌入式系统上开机后的时间不准确比如是1970,而后会变成1999或者其他时间
//会在getDiffValue函数执行很久很久
startTime = QDateTime::currentDateTime();
@@ -84,6 +84,10 @@ void SaveRunTime::start()
void SaveRunTime::stop()
{
if (!timerSave->isActive()) {
return;
}
timerSave->stop();
}
@@ -116,7 +120,7 @@ void SaveRunTime::initLog()
QString line = strID + strStartTime + strEndTime + strRunTime;
QTextStream stream(&file);
stream << line << NEWLINE;
stream << line << "\n";
file.close();
lastID = 0;
}
@@ -154,7 +158,7 @@ void SaveRunTime::appendLog()
QString line = strID + strStartTime + strEndTime + strRunTime;
QTextStream stream(&file);
stream << line << NEWLINE;
stream << line << "\n";
file.close();
}
}
@@ -205,7 +209,7 @@ void SaveRunTime::saveLog()
content[content.count() - 1] = lastLine;
QTextStream stream(&file);
stream << content.join("") << NEWLINE;
stream << content.join("") << "\n";
file.close();
}
}