.dat Files
-
Currently trying to read and print a .dat file and have no idea where to start. My goal is to display each line of data on a QLabel. I had this code, but nothing happened.
QFile inputFile(QString("/airplane.dat")); inputFile.open(QIODevice::ReadOnly); if (!inputFile.isOpen()){ return; } QTextStream stream(&inputFile); for (QString line = stream.readLine(); !line.isNull(); line = stream.readLine()) { ui->datText->setText(line); qApp->processEvents(); }
Here is what the .dat file looks like:
Any idea on what to do?
-
@WesLow said in .dat Files:
ui->datText->setText("line");
Why do you set a text 'line' instead the content of the variable
line
in here? -
@Christian-Ehrlicher Did that as a test to make sure it wasn't a GUI issue; forgot to change it back. Let me edit that really quickly...
-
@WesLow I see processEvent calls, multi calls to readline() where the previous data is thrown away (unused).
your dat file seems to be a simple text file, why don't you simply do:
QTextStream stream(&inputFile); auto myText = stream.readAll(); //Console output for tests qDebug() << myText; //Set text for ui ui->datText->setText(myText);
-
@WesLow said in .dat Files:
My goal is to display each line of data on a QLabel
First the file just looks like text. Maybe with tabs where there are multiple spaces, we don't know; what program were you using to view that file when you took the screenshot?
So you just need to do what @J-Hilk says, nothing fancy.
If that works, you will see a single multi-line
QLabel
(if that is what yourui->datText
is).If you then want to do something about breaking it into "each line", I do not suggest you create and place a
QLabel
for each line. Unless it's just a learning exercise. A QListView might be a preferable widget to use.P.S.
I had this code, but nothing happened.
I suspect something did happen. But quickly...!
ui->datText->setText(line); qApp->processEvents();
You should be left at the end with whatever the final line in the file is. I think you are expecting to see each line at a time running past, but with one
qApp->processEvents();
per iteration I think it will happen much faster than your eye :)