How can I format a data table in Qt?
-
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 185639and 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 185The 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?
-
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 185639and 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 185The 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?
@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 -
@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 117611680and change it to:
Rest 1738 0.05 70933 117611It would be very helpful if someone showed me an example
-
@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 117611680and change it to:
Rest 1738 0.05 70933 117611It would be very helpful if someone showed me an example
@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). -
@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(); } -
@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(); }@viniltc said in How can I format a data table in Qt?:
QTextStream instream(& readfile); readfile.close();You are closing the underlying
readfilebefore reading from it. Movereadfile.close()to after the loop which reads from it/theQTextStreamwrapper.