Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I format a data table in Qt?
Qt 6.11 is out! See what's new in the release blog

How can I format a data table in Qt?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 576 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    viniltc
    wrote on last edited by
    #1

    Hi,

    I am not familiar with file formatting in Qt. I have a txt file containing the data log from a device, as below:

    === USAGE_LOG ===
    Phase	count	min time ms	max time ms	total time ms
    0	1738	5	70933790	117611680
    1	376	18	4103927	7104930
    2	339	432	105935	2167343
    3	288	298	96749	1955551
    4	261	225	79872	1051601
    5	94	639	2000	185639
    
    

    and I need to modify it ideally would like:

    === USAGE_LOG ===
    Phase	  count  	min time 	max time  total time 
    Rest	  1738	        0.05	        70933	  117611
    Open_1	  376	        0.018	        4103	  7104
    Move	  339	        0.432	        105       2167
    Grip_1	  288	        0.298	        96	  1955
    Grip_2    261	        0.225	        79	  1051
    Open_3	  94	        0.639	        2	  185
    
    

    The first column changed from numbers to words, and all the time columns are changed from milliseconds to seconds. Can anyone suggest an efficient way to do this?

    jsulmJ 1 Reply Last reply
    0
    • V viniltc

      Hi,

      I am not familiar with file formatting in Qt. I have a txt file containing the data log from a device, as below:

      === USAGE_LOG ===
      Phase	count	min time ms	max time ms	total time ms
      0	1738	5	70933790	117611680
      1	376	18	4103927	7104930
      2	339	432	105935	2167343
      3	288	298	96749	1955551
      4	261	225	79872	1051601
      5	94	639	2000	185639
      
      

      and I need to modify it ideally would like:

      === USAGE_LOG ===
      Phase	  count  	min time 	max time  total time 
      Rest	  1738	        0.05	        70933	  117611
      Open_1	  376	        0.018	        4103	  7104
      Move	  339	        0.432	        105       2167
      Grip_1	  288	        0.298	        96	  1955
      Grip_2    261	        0.225	        79	  1051
      Open_3	  94	        0.639	        2	  185
      
      

      The first column changed from numbers to words, and all the time columns are changed from milliseconds to seconds. Can anyone suggest an efficient way to do this?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @viniltc What exactly is the problem?
      Read the file line by line and in each line change what you need to change and write it to another file.
      Writing to a text file can be done using https://doc.qt.io/qt-6/qtextstream.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • V Offline
        V Offline
        viniltc
        wrote on last edited by
        #3

        @jsulm Hi.

        I can read line by line whole content of the file to a text browser as follows:

            QString fname = "logfile.txt";
            QFile file(fname);
        
            file.open(QIODevice::ReadOnly|QIODevice::Text);
        
            QTextStream instream(& file);
        
            while (!instream.atEnd()){
                QString line = instream.readLine();
                ui->textBrowser->append(line); 
            }
        
        
            file.close();
        

        But I'm not sure how to read a particular line, for example. Read 3rd line:

        0	1738	5	70933790	117611680
        

        and change it to:

        Rest	  1738	        0.05	        70933	  117611
        

        It would be very helpful if someone showed me an example

        jsulmJ 1 Reply Last reply
        0
        • V viniltc

          @jsulm Hi.

          I can read line by line whole content of the file to a text browser as follows:

              QString fname = "logfile.txt";
              QFile file(fname);
          
              file.open(QIODevice::ReadOnly|QIODevice::Text);
          
              QTextStream instream(& file);
          
              while (!instream.atEnd()){
                  QString line = instream.readLine();
                  ui->textBrowser->append(line); 
              }
          
          
              file.close();
          

          But I'm not sure how to read a particular line, for example. Read 3rd line:

          0	1738	5	70933790	117611680
          

          and change it to:

          Rest	  1738	        0.05	        70933	  117611
          

          It would be very helpful if someone showed me an example

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @viniltc said in How can I format a data table in Qt?:

          But I'm not sure how to read a particular line, for example. Read 3rd line:

          Read lines until you got line number 3 (counting lines should really not be an issue).
          "and change it to" - if you know which columns you want to change you can use https://doc.qt.io/qt-6/qstring.html#split to split the line, then change the columns you want to change and merge the substrings into one string again (https://doc.qt.io/qt-6/qstringlist.html#join for example).

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          5
          • V Offline
            V Offline
            viniltc
            wrote on last edited by
            #5

            @jsulm Thanks.

            I have a basic question:

            I opened an old log file in the readonly mode and stored content in QTextStream, and closed it

            I opened another file to write and tried to read line by line and write it into the new file. As an initial step, I just want to see each line read on Debug, but nothing is showing. Any idea what's wrong here?

                QString oldfilename = "log_file";
                QString newfilename = "new_log_file";
            
                QString path1 = QCoreApplication::applicationDirPath()+"/"+oldfilename +".txt";
                QString path2 = QCoreApplication::applicationDirPath()+"/"+newfilename+".txt";
            
                QFile readfile(path1);
                if(!readfile.open(QIODevice::ReadOnly | QIODevice::Text)){
            
                    qDebug() << "Error opening file: "<<readfile.errorString();
                }
            
                QTextStream instream(& readfile);
                readfile.close();
            
            
                QFile writefile(path2);
                if(file.open(QIODevice::WriteOnly | QIODevice::Text))
                {
                    int nb_line(0);
                    while(!instream.atEnd())
                    {
                        QString line = instream.readLine();
                        
                       if(nb_line == 1 )
                        {
                           qDebug () << line ;
                        }
                        if(nb_line == 2 )
                        {
                            qDebug () << line ;
                        }
            
                        if(nb_line == 3 )
                        {
                           qDebug () << line ;
                        }
                        
                    ++nb_line;
                   
                      }
                    
                 writefile.close();
                }
            
            
            
            
            JonBJ 1 Reply Last reply
            0
            • V viniltc

              @jsulm Thanks.

              I have a basic question:

              I opened an old log file in the readonly mode and stored content in QTextStream, and closed it

              I opened another file to write and tried to read line by line and write it into the new file. As an initial step, I just want to see each line read on Debug, but nothing is showing. Any idea what's wrong here?

                  QString oldfilename = "log_file";
                  QString newfilename = "new_log_file";
              
                  QString path1 = QCoreApplication::applicationDirPath()+"/"+oldfilename +".txt";
                  QString path2 = QCoreApplication::applicationDirPath()+"/"+newfilename+".txt";
              
                  QFile readfile(path1);
                  if(!readfile.open(QIODevice::ReadOnly | QIODevice::Text)){
              
                      qDebug() << "Error opening file: "<<readfile.errorString();
                  }
              
                  QTextStream instream(& readfile);
                  readfile.close();
              
              
                  QFile writefile(path2);
                  if(file.open(QIODevice::WriteOnly | QIODevice::Text))
                  {
                      int nb_line(0);
                      while(!instream.atEnd())
                      {
                          QString line = instream.readLine();
                          
                         if(nb_line == 1 )
                          {
                             qDebug () << line ;
                          }
                          if(nb_line == 2 )
                          {
                              qDebug () << line ;
                          }
              
                          if(nb_line == 3 )
                          {
                             qDebug () << line ;
                          }
                          
                      ++nb_line;
                     
                        }
                      
                   writefile.close();
                  }
              
              
              
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @viniltc said in How can I format a data table in Qt?:

              QTextStream instream(& readfile);
              readfile.close();
              

              You are closing the underlying readfile before reading from it. Move readfile.close() to after the loop which reads from it/the QTextStream wrapper.

              1 Reply Last reply
              3

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved