#include "threadread.h" threadRead::threadRead(QObject *parent) : QThread{parent} { } bool threadRead::isNumeric(QString data) { // 正则表达式匹配数字和可选的正负号 QRegularExpression pattern("^[+-]?([1-9][0-9]*|0)[.]?[0-9]*$"); QRegularExpressionMatch patternMatch =pattern.match(data); // 使用std::regex_match来检查字符串是否完全匹配模式 return patternMatch.hasMatch(); } void threadRead::run() { //正则表达式 //时间 QRegularExpression dateTimeRegex("([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{3})"); QRegularExpressionMatch matchDateTime; //基带 QRegularExpression jdRegex("\\[.*(\\d)\\]"); QRegularExpressionMatch matchLogLevel; //初始化数据 for (auto &i: Gloab::g_param) { i.clear(); } //清除下拉框数据 emit clearCombo(); if(!Gloab::namelist.empty()){ Gloab::namelist.clear(); Gloab::completer->deleteLater(); } //设置状态栏 emit showStatusbar("正在打开文件"); //读取文件 QTextStream in(&Gloab::file); while (!in.atEnd()) { //读取一行 QString line = in.readLine(); //更新进度条 Gloab::bytesRead += line.size(); int percent = Gloab::bytesRead * 150 / Gloab::fileSize; emit setProgressBar(percent); /* 匹配数据 */ //匹配日期时间 QMap 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 (Gloab::temData.isEmpty()) { emit setBeginTime(QDateTime::fromString(temtime, "yyyy-MM-dd hh:mm:ss:zzz")); } Gloab::temData.clear(); Gloab::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 (!Gloab::namelist.contains(tem[0])) { Gloab::namelist[tem[0]]=isNumeric(tem[1]); if (tem[0].contains("版本号")) { Gloab::namelist[tem[0]] = false; } } Gloab::temData[tem[0]] = tem[1]; } //记录数据 Gloab::g_param[JD].append(Gloab::temData); } //设置结束时间 emit setEndTime(QDateTime::fromString(Gloab::temData["时间"], "yyyy-MM-dd hh:mm:ss:zzz")); //设置状态栏 emit showStatusbar("打开成功:" + Gloab::fileName); //添加下拉框数据 emit addCombo(Gloab::namelist.keys()); //关闭 emit closeProg(); Gloab::file.close(); Gloab::temData.clear(); quit(); }