Search code examples
c++qtqt5qtexteditqfile

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


I have a text file as follows:

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

The code is as follows:

#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;
}

I performed button and lineedits in form.After, I splitted time ranges hh:mm:ss format.Because I have to do error checking on time ranges.

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();

I read text file and splitted file.I have to need compare time ranges in text file with time ranges that entered by user.

    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;

}

In following code,I did error checking as I mentioned above.And,I tried display records at specified time intervals.But when I run the code,nothing seems in textbrowser that I created to display records.I don't understand why this is happening.Also,no error occurs.For example,user entered 05:59:57 to first lineedit and 06:00:46 to second lineedit.Then user clicked button to display records at this time intervals.I want to display records from 05:59:57 to 06:00:46.But,there are no records in textbrowser.Nothing seems.HOW CAN I SOLVE THIS PROBLEM?

      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;

  }
}

Solution

  • To compare times we must use the QTime class, this class provides implements the time comparison operators. Along with this it is advisable to use QTimeEdit which provides a suitable widget for these parameters so I recommend you to change them instead of using QLineEdit.

    An appropriate format has to be established, for this the following is done:

    #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));
    }
    

    Then in the slot that is called when you press the button we do the task of filtering for it we get the times, we do some checks if the limits are adequate. After we read the file and we get the first 8 characters we convert it in time and if it is valid and it is between the times we change the flag, if instead it is not valid we leave it as it was, all of the above was implemented in the following code:

    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();
                    }
                }
            }
        }
    }
    

    The following image shows the result:

    enter image description here

    The complete example can be found in the following link