完成文件读取

This commit is contained in:
2024-09-18 10:25:29 +08:00
parent f4e058c009
commit d9cdba7cb7
10 changed files with 435 additions and 57 deletions

View File

@ -19,6 +19,9 @@ set(PROJECT_SOURCES
mainwindow.cpp mainwindow.cpp
mainwindow.h mainwindow.h
mainwindow.ui mainwindow.ui
global.h
readthread.cpp
readthread.h
) )
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
@ -40,7 +43,7 @@ else()
else() else()
add_executable(ReadLog add_executable(ReadLog
${PROJECT_SOURCES} ${PROJECT_SOURCES}
qcustomplot.cpp qcustomplot.h
) )
endif() endif()
endif() endif()

View File

@ -10,17 +10,23 @@ CONFIG += c++11
SOURCES += \ SOURCES += \
main.cpp \ main.cpp \
mainwindow.cpp mainwindow.cpp \
# qcustomplot.cpp qcustomplot.cpp \
readthread.cpp
HEADERS += \ HEADERS += \
mainwindow.h global.h \
# qcustomplot.h mainwindow.h \
qcustomplot.h \
readthread.h
FORMS += \ FORMS += \
mainwindow.ui mainwindow.ui
RC_ICONS = logo.ico
# Default rules for deployment. # Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target !isEmpty(target.path): INSTALLS += target

30
global.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef READLOG_GLOBAL_H
#define READLOG_GLOBAL_H
#include <QString>
#include <QVector>
#include <QMap>
#include <QFile>
#include <QCompleter>
extern QVector<QMap<QString,QString>> g_param[4]; //存储4条基带的数据
extern QMap<QString,bool> namelist; //存储数据名以及数据是否是数字
extern QFile file; //打开文件
extern QCompleter *completer;
#endif //READLOG_GLOBAL_H

BIN
logo.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

View File

@ -1,11 +1,36 @@
#include "mainwindow.h" #include "mainwindow.h"
#include <QApplication> #include <QApplication>
#include <QTextCodec>
#include <QDebug> #include <QDebug>
#include "global.h"
int main(int argc, char *argv[])
{ //初始化数据
QVector<QMap<QString, QString>> g_param[4];
QMap<QString, bool> namelist;
QFile file;
QCompleter *completer;
int main(int argc, char *argv[]) {
//设置logo
QApplication::setWindowIcon(QIcon("logo.ico"));
//放大窗口
// qputenv("QT_SCALE_FACTOR", "2.0");
//设置使用UTF8字符集正确显示中文
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForLocale(codec);
//运行程序
QApplication a(argc, argv); QApplication a(argc, argv);
/* 获取分辨率
QDesktopWidget *desktopWidget = QApplication::desktop();
//获取可用桌面大小
QRect deskRect = desktopWidget->availableGeometry();
qDebug() << deskRect.width() << "x" << deskRect.height();
//获取设备屏幕大小
QRect screenRect = desktopWidget->screenGeometry();
qDebug() << screenRect.width() << "x" << screenRect.height();*/
MainWindow w; MainWindow w;
w.show(); w.show();
return a.exec(); return a.exec();

View File

@ -1,22 +1,99 @@
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
#include <QCompleter>
#include <QLabel>
#include "mainwindow.h" #include "mainwindow.h"
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include <QCompleter>
#include <QListView>
#include <QStyledItemDelegate>
#include <QLineEdit>
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent), ui(new Ui::MainWindow), readThread(new ReadThread) {
, ui(new Ui::MainWindow)
{
ui->setupUi(this); ui->setupUi(this);
// 创建版本号标签并添加到状态栏的右端
QLabel *m_versionLabel = new QLabel(tr("v1.2.0"), this);
ui->statusbar->addPermanentWidget(m_versionLabel);
//隐藏进度条
ui->progressBar->close();
/*信号和槽*/
//readThread
connect(readThread, &ReadThread::clearCombo, this, &MainWindow::clearCombo);
connect(readThread, &ReadThread::showStatusbar, this, &MainWindow::showStatusbar);
connect(readThread, &ReadThread::setProgressBar, this, &MainWindow::setProgressBar);
connect(readThread, &ReadThread::setBeginTime, this, &MainWindow::setBeginTime);
connect(readThread, &ReadThread::setEndTime, this, &MainWindow::setEndTime);
connect(readThread, &ReadThread::addCombo, this, &MainWindow::addCombo);
connect(readThread, &ReadThread::closeProg, this, &MainWindow::closeProg);
} }
MainWindow::~MainWindow() MainWindow::~MainWindow() {
{
delete ui; delete ui;
} }
void MainWindow::on_openFileBtn_clicked() {
//选择文件,并获取文件名
QString fileName = QFileDialog::getOpenFileName();
//打开文件
file.setFileName(fileName);
//打开失败
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::warning(this, "错误", "文件打开失败");
ui->statusbar->clearMessage();
clearCombo();
return;
}
//获取文件大小
qint64 fileSize = file.size();
//设置进度条
ui->progressBar->setMaximum((int) fileSize);
ui->progressBar->setValue(0);
ui->progressBar->show();
//执行读取
readThread->start();
}
void MainWindow::on_timeCustomBtn_clicked() {
ui->progressBar->close();
}
void MainWindow::on_dataCustomBtn_clicked() {
}
void MainWindow::clearCombo() {
ui->comboBox->clear();
}
void MainWindow::showStatusbar(const QString &str) {
ui->statusbar->showMessage(str);
}
void MainWindow::setProgressBar(int bytesRead) {
ui->progressBar->setValue(bytesRead);
}
void MainWindow::setBeginTime(const QDateTime &beginTime) {
ui->beginTimeEdit->setDateTime(beginTime);
}
void MainWindow::setEndTime(const QDateTime &endTime) {
ui->endTimeEdit->setDateTime(endTime);
}
void MainWindow::addCombo(const QStringList &names) {
//设置下拉框数据
ui->comboBox->addItems(names);
//根据输入匹配下拉框
completer = new QCompleter(names);
completer->setFilterMode(Qt::MatchContains);
ui->comboBox->setCompleter(completer);
}
void MainWindow::closeProg() {
ui->progressBar->close();
}

View File

@ -3,22 +3,59 @@
#include <QMainWindow> #include <QMainWindow>
#include "global.h"
#include "readthread.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { namespace Ui {
class MainWindow; class MainWindow;
} }
QT_END_NAMESPACE QT_END_NAMESPACE
class MainWindow : public QMainWindow class MainWindow : public QMainWindow {
{ Q_OBJECT
Q_OBJECT
public: public:
MainWindow(QWidget *parent = nullptr); explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
~MainWindow() override;
private slots:
/*打开文件*/
void on_openFileBtn_clicked();
/*绘制时间图*/
void on_timeCustomBtn_clicked();
/*绘制数据变化图*/
void on_dataCustomBtn_clicked();
/*清空下拉框*/
void clearCombo();
/*设置底部左侧状态栏文字*/
void showStatusbar(const QString &str);
/*设置进度条进度*/
void setProgressBar(int bytesRead);
/*设置开始时间*/
void setBeginTime(const QDateTime &beginTime);
/*设置结束时间*/
void setEndTime(const QDateTime &endTime);
/*设置下拉框*/
void addCombo(const QStringList &names);
/*关闭进度条*/
void closeProg();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
ReadThread *readThread;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -2,6 +2,12 @@
<ui version="4.0"> <ui version="4.0">
<class>MainWindow</class> <class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow"> <widget class="QMainWindow" name="MainWindow">
<property name="windowModality">
<enum>Qt::NonModal</enum>
</property>
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
@ -10,13 +16,16 @@
<height>700</height> <height>700</height>
</rect> </rect>
</property> </property>
<property name="mouseTracking">
<bool>false</bool>
</property>
<property name="windowTitle"> <property name="windowTitle">
<string>MainWindow</string> <string>日志读取</string>
</property> </property>
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout_16"> <layout class="QGridLayout" name="gridLayout_16">
<item row="6" column="0"> <item row="6" column="0">
<widget class="QGroupBox" name="groupBox_3"> <widget class="QGroupBox" name="groupBox_end">
<property name="font"> <property name="font">
<font> <font>
<family>宋体</family> <family>宋体</family>
@ -28,7 +37,7 @@
</property> </property>
<layout class="QGridLayout" name="gridLayout_14"> <layout class="QGridLayout" name="gridLayout_14">
<item row="0" column="0"> <item row="0" column="0">
<widget class="QDateTimeEdit" name="dateTimeEdit_2"> <widget class="QDateTimeEdit" name="endTimeEdit">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Expanding"> <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -50,16 +59,22 @@
</widget> </widget>
</item> </item>
<item row="10" column="0"> <item row="10" column="0">
<widget class="QWidget" name="widget" native="true"> <widget class="QWidget" name="progressWidget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>0</width> <width>0</width>
<height>40</height> <height>45</height>
</size> </size>
</property> </property>
<layout class="QGridLayout" name="gridLayout_13"> <layout class="QGridLayout" name="gridLayout_13">
<item row="0" column="0"> <item row="0" column="0">
<widget class="QProgressBar" name="progressBar_6"> <widget class="QProgressBar" name="progressBar">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Maximum"> <sizepolicy hsizetype="Expanding" vsizetype="Maximum">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -69,7 +84,7 @@
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>16777215</width> <width>16777215</width>
<height>16777215</height> <height>25</height>
</size> </size>
</property> </property>
<property name="value"> <property name="value">
@ -87,7 +102,7 @@
</widget> </widget>
</item> </item>
<item row="5" column="0"> <item row="5" column="0">
<widget class="QGroupBox" name="groupBox_2"> <widget class="QGroupBox" name="groupBox_begin">
<property name="font"> <property name="font">
<font> <font>
<family>宋体</family> <family>宋体</family>
@ -99,7 +114,7 @@
</property> </property>
<layout class="QGridLayout" name="gridLayout_12"> <layout class="QGridLayout" name="gridLayout_12">
<item row="0" column="0"> <item row="0" column="0">
<widget class="QDateTimeEdit" name="dateTimeEdit"> <widget class="QDateTimeEdit" name="beginTimeEdit">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Expanding"> <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -121,13 +136,19 @@
</widget> </widget>
</item> </item>
<item row="4" column="0"> <item row="4" column="0">
<widget class="QGroupBox" name="groupBox"> <widget class="QGroupBox" name="groupBox_jd">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding"> <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch> <horstretch>0</horstretch>
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="minimumSize">
<size>
<width>0</width>
<height>70</height>
</size>
</property>
<property name="font"> <property name="font">
<font> <font>
<family>宋体</family> <family>宋体</family>
@ -141,39 +162,39 @@
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set> <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property> </property>
<layout class="QGridLayout" name="gridLayout_11"> <layout class="QGridLayout" name="gridLayout_11">
<item row="0" column="0">
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string>基带 1</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QCheckBox" name="checkBox_2">
<property name="text">
<string>基带 2</string>
</property>
</widget>
</item>
<item row="0" column="2"> <item row="0" column="2">
<widget class="QCheckBox" name="checkBox_3"> <widget class="QCheckBox" name="jd3">
<property name="text"> <property name="text">
<string>基带 3</string> <string>基带 3</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1">
<widget class="QCheckBox" name="jd2">
<property name="text">
<string>基带 2</string>
</property>
</widget>
</item>
<item row="0" column="3"> <item row="0" column="3">
<widget class="QCheckBox" name="checkBox_4"> <widget class="QCheckBox" name="jd4">
<property name="text"> <property name="text">
<string>基带 4</string> <string>基带 4</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="0">
<widget class="QCheckBox" name="jd1">
<property name="text">
<string>基带 1</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>
<item row="0" column="0"> <item row="0" column="0">
<spacer name="verticalSpacer_2"> <spacer name="verticalSpacer_1">
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
</property> </property>
@ -186,7 +207,7 @@
</spacer> </spacer>
</item> </item>
<item row="7" column="0"> <item row="7" column="0">
<spacer name="verticalSpacer"> <spacer name="verticalSpacer_3">
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
</property> </property>
@ -224,7 +245,7 @@
</widget> </widget>
</item> </item>
<item row="3" column="0"> <item row="3" column="0">
<widget class="QGroupBox" name="groupBox_4"> <widget class="QGroupBox" name="groupBox_data">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding"> <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -270,7 +291,7 @@
</widget> </widget>
</item> </item>
<item row="8" column="0"> <item row="8" column="0">
<widget class="QPushButton" name="pushButton_2"> <widget class="QPushButton" name="timeCustomBtn">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Expanding"> <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -295,7 +316,7 @@
</widget> </widget>
</item> </item>
<item row="9" column="0"> <item row="9" column="0">
<widget class="QPushButton" name="pushButton_3"> <widget class="QPushButton" name="dataCustomBtn">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Expanding"> <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -320,7 +341,7 @@
</widget> </widget>
</item> </item>
<item row="2" column="0"> <item row="2" column="0">
<spacer name="verticalSpacer_3"> <spacer name="verticalSpacer_2">
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
</property> </property>

130
readthread.cpp Normal file
View File

@ -0,0 +1,130 @@
#include <QTextStream>
#include "readthread.h"
ReadThread::ReadThread(QObject *parent)
: QThread{parent} {
/*正则表达式*/
//时间
dateTimeRegex.setPattern("([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{3})");
//基带
jdRegex.setPattern(R"(\[.*(\d)\])");
//匹配数字和可选的正负号
pattern.setPattern("^[+-]?([1-9][0-9]*|0)[.]?[0-9]*$");
//科学计数法
kexue.setPattern("^[0-9].[0-9]e[+-][0-9][0-9]");
}
bool ReadThread::isNumeric(QString data) {
bool flag = false;
patternMatch = pattern.match(data);
if (patternMatch.hasMatch()) {
flag = true;
}
kexueMatch = kexue.match(data);
if (kexueMatch.hasMatch()) {
flag = true;
}
return flag;
}
void ReadThread::run() {
//初始化数据
int bytesRead = 0; //进度条数值
for (auto &i: g_param) {
i.clear();
}
QMap<QString, QString> temData;
bool isBeginTime = true;
//清除下拉框数据
emit clearCombo();
if (!namelist.empty()) {
namelist.clear();
completer->deleteLater();
}
//设置状态栏
emit showStatusbar("正在打开文件");
//读取文件
QTextStream in(&file);
while (!in.atEnd()) {
//读取一行
QString line = in.readLine();
//更新进度条
bytesRead += line.size();
emit setProgressBar(bytesRead * 2);
/* 匹配数据 */
//匹配日期时间
QMap<QString, QString> temMap;
int JD;
QString temtime;
QString temjd;
matchDateTime = dateTimeRegex.match(line);
if (matchDateTime.hasMatch()) {
temtime = matchDateTime.captured(1);
temMap["时间"] = temtime;
} else {
continue;
}
//匹配基带
matchLogLevel = jdRegex.match(line);
if (matchLogLevel.hasMatch()) {
temjd = matchLogLevel.captured(1);
temMap["基带"] = temjd;
JD = temjd.toInt() - 1;
} else {
continue;
}
//设置开始时间
if (isBeginTime) {
emit setBeginTime(QDateTime::fromString(temtime, "yyyy-MM-dd hh:mm:ss:zzz"));
isBeginTime = false;
}
temData.clear();
temData["时间"] = temtime;
//将左右拆分
QStringList dataParts = line.split(']');
//判断右侧是否有数据
if (dataParts[1].isEmpty()) {
continue;
}
//将右侧数据拆分
QStringList data = dataParts[1].split(',');
for (int i = 0; i < data.size(); i++) {
//将每一条数据拆为数据名和值
QStringList tem = data[i].split('=');
if (tem.size() < 2) {
continue;
}
if (!namelist.contains(tem[0])) {
namelist[tem[0]] = isNumeric(tem[1]);
if (tem[0].contains("版本号")) {
namelist[tem[0]] = false;
}
}
temData[tem[0]] = tem[1];
}
//记录数据
g_param[JD].append(temData);
}
//设置结束时间
emit setEndTime(QDateTime::fromString(temData["时间"], "yyyy-MM-dd hh:mm:ss:zzz"));
//设置状态栏
emit showStatusbar("打开成功:" + file.fileName());
//添加下拉框数据
emit addCombo(namelist.keys());
//关闭
emit closeProg();
file.close();
temData.clear();
quit();
}

49
readthread.h Normal file
View File

@ -0,0 +1,49 @@
#ifndef READTHREAD_H
#define READTHREAD_H
#include <QThread>
#include <QFile>
#include <QRegularExpression>
#include <QDateTime>
#include "global.h"
class ReadThread : public QThread {
Q_OBJECT
public:
explicit ReadThread(QObject *parent = nullptr);
bool isNumeric(QString data);
signals:
void clearCombo();
void showStatusbar(QString str);
void setProgressBar(int bytesRead);
void setBeginTime(QDateTime beginTime);
void setEndTime(QDateTime beginTime);
void addCombo(QStringList names);
void closeProg();
protected:
void run() Q_DECL_OVERRIDE;
private:
QRegularExpression dateTimeRegex;
QRegularExpressionMatch matchDateTime;
QRegularExpression jdRegex;
QRegularExpressionMatch matchLogLevel;
QRegularExpression pattern;
QRegularExpressionMatch patternMatch;
QRegularExpression kexue;
QRegularExpressionMatch kexueMatch;
};
#endif // READTHREAD_H