如何将文本文件中的时间范围与用户在 qt 中输入的时间范围进行比较?

How to compare time ranges in text file with time ranges that entered by user in qt?

我有一个text file如下:

05:59:57 - [0x1010001]05:59:57 - (2576) WRITING TO NON-VOLATILE MEMORY IS DONE
06:00:00 - [0x1010001]06:00:00 - (23371)  T_Check_Buddy !!!  
06:00:00 - DMA:310127952,01,REQ:BRDTIM 82 07 83 29 05 0f 04 12 06 00 
06:00:00 - 
06:00:00 - EvmTbl............
06:00:00 - Maintenancing & Filling VboxTbl...DONE
06:00:01 - DMA:310128070,01,IND:KTSPER 96 10 85 fc 00 28 58 
06:00:01 - DMA:310128071,01,REQ:KTSIDK 82 10 85 fc 81 00 47 02 
06:00:01 - DMA:310128091,01,IND:KTSPER 96 10 86 fc 00 28 58 
06:00:01 - DMA:310128091,01,REQ:KTSIDK 82 10 86 fc 81 00 47 02 
06:00:02 - SIP:310129384, REQ:     KINFO     To:1800 To-Host:192.168.178.230 Ktext: 02 78 0e
06:00:30 - [0x1010001]06:00:30 - (23371)  T_Check_Buddy !!!  
06:00:32 - SIP:310159385, REQ:     KINFO     To:1800 To-Host:192.168.178.230 Ktext: 02 78 0e
06:00:46 - IPT:310173571,255,IND: CONFIG 87 03 4c 43 4e

代码如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include<QFile>
#include<QTextStream>
#include<QStringList>
#include<QDebug>
#include<QMessageBox>

using namespace std;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
   ui->setupUi(this);
}

MainWindow::~MainWindow()
{
   delete ui;
}

我在 form.After 中执行了按钮和行编辑,我拆分了时间范围 hh:mm:ss format.Because 我必须对时间范围进行错误检查。

void MainWindow::on_pushButton_3_clicked()
{
   QString output;
   QString line;
   QStringList splitted;
   QString times;

   int sayac1=0,sayac2=0;
   bool control1=false;
   bool control2=false;


   QString firsttime=ui->lineEdit->text();  //first time range that entered 
   by user.
   QStringList list1=firsttime.split(":");  //Girilen time split edildi.

   if (list1.size() < 3)
     QMessageBox::warning(this,"LIST","ALANLAR BOŞ BIRAKILAMAZ!");

   QString hour1=list1[0];
   hour1.toInt();

   QString minute1=list1[1];
   minute1.toInt();

   QString second1=list1[2];
   second1.toInt();


   QString secondtime=ui->lineEdit_2->text();  //second time range that 
   entered by user. 
   QStringList list2=secondtime.split(":");    //Girilen aralık split 
   edildi.

   if(list2.size() < 3)
     QMessageBox::warning(this,"LIST","ALANLAR BOŞ BIRAKILAMAZ!");


  QString hour2=list2[0];
  hour2.toInt();

  QString minute2=list2[1];
  minute2.toInt();

  QString second2=list2[2];
  second2.toInt();

我读取文本文件并拆分 file.I 需要将文本文件中的时间范围与用户输入的时间范围进行比较。

    QFile file("C:\kaynak\naile.txt");

       if(file.open(QIODevice::ReadOnly | QIODevice::Text)){

           QTextStream in(&file);

           while(!in.atEnd())
           {

               line = in.readLine()+"\n";
               output.append(line);


               if(line.contains(" - ")){
                   splitted=line.split(" - ");
                   times=times+" "+splitted[0];
               }

               if(splitted[0]!=firsttime && control1==false){
                  sayac1 = sayac1+1;
               }
               else
                  control1=true;


               if(splitted[0]!=secondtime && control2==false){
                  sayac2++;
               }
               else
                  control2=true;

}

在下面的代码中,我按照我提到的 above.And 进行了错误检查,当我 运行 代码时,我尝试在指定时间 intervals.But 显示记录,我在 textbrowser 中似乎没有任何内容创建显示 records.I 不明白为什么这是 happening.Also,没有错误 occurs.For 示例,用户在第一行编辑中输入 05:59:57,在第二行编辑中输入 06:00:46 lineedit.Then用户此时点击按钮显示记录intervals.I想显示05:59:57到06:00:46的记录。但是textbrowser.Nothingseems.HOW中没有记录] 我能解决这个问题吗?

      if(hour1<=hour2){

        int i;
        QString list;
        QString newline=splitted[0]+" - "+splitted[1];
        list.append(newline);

        if(times.contains(firsttime) && times.contains(secondtime)){

           for(i=sayac1+1;i<=sayac2+1;i++){
                ui->textBrowser_3->setText(list.at(i));
           }

       }

        else
           QMessageBox::warning(this,"LIST","GİRDİĞİNİZ ZAMAN ARALIKLARI 
           EŞLEŞMİYOR!");
        }


        if(hour1>hour2)
           QMessageBox::warning(this,"LIST","İKİNCİ SAAT BİRİNCİDEN BÜYÜK 
           OLAMAZ!");



           file.close();
          // return output;

  }
}

要比较时间我们必须使用QTime class,这个class提供了实现时间比较运算符。除此之外,建议使用 QTimeEdit 为这些参数提供合适的小部件,因此我建议您更改它们而不是使用 QLineEdit.

必须建立适当的格式,为此需要执行以下操作:

#define FORMAT  "hh:mm:ss"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->fromTimeEdit->setDisplayFormat(FORMAT);
    ui->toTimeEdit->setDisplayFormat(FORMAT);
    ui->toTimeEdit->setTime(QTime::fromString("23:59:59", FORMAT));
}

然后在您按下按钮时调用的槽中,我们执行过滤任务,我们得到时间,我们做一些检查是否限制足够。在我们读取文件并获得前 8 个字符后,我们及时转换它,如果它有效并且它在我们更改标志的时间之间,如果它无效我们保持原样,以上所有在以下代码中实现:

void MainWindow::on_pushButton_clicked()
{
    const QTime from =  ui->fromTimeEdit->time();
    const QTime to = ui->toTimeEdit->time();

    bool copyLog = false;
    if(from <= to){
        QFile file("/path/of/kaynak.log");
        ui->textEdit->clear();
        if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
            QTextStream in(&file);
            while(!in.atEnd()){
                const QString line = in.readLine();
                const QTime timeLog = QTime::fromString(line.left(8), FORMAT);
                if(timeLog.isValid()){
                    copyLog = timeLog >= from && timeLog <= to;
                }
                if(copyLog){
                    ui->textEdit->append(line);
                    QApplication::processEvents();
                }
            }
        }
    }
}

结果如下图所示:

完整的例子可以在下面找到link