PDA

View Full Version : loading a textfile in a qlabel



harakiri
11th June 2007, 17:09
hi,

can anyone tell me why the following code doesn't display the content of the given textfile in the QLabel ?



QFile trainResult("26.train");
QTextStream in(&trainResult);

trainLabel->setText(in.readAll());
trainLabel->adjustSize();



26.train is a small textfile, trainLabel a simple QLabel. trainLabel->setText("abc"); works fine...

thanks in advance

high_flyer
11th June 2007, 18:04
you didn't open the file.
Also, the 'Qt' tags are not for code, but for reference to the docs.
Use the code tags '#' for code.

harakiri
11th June 2007, 21:17
you didn't open the file.


and how do i open it in this case?

jpn
11th June 2007, 21:39
and how do i open it in this case?
See the examples in QFile (http://doc.trolltech.com/4.3/qfile#using-streams-to-read-files) and QTextStream (http://doc.trolltech.com/4.3/qtextstream.html#details) docs.

harakiri
11th June 2007, 23:33
See the examples in QFile (http://doc.trolltech.com/4.3/qfile#using-streams-to-read-files) and QTextStream (http://doc.trolltech.com/4.3/qtextstream.html#details) docs.

isn't there anyone who can help me instead of posting links to the docu i've already read?

i tried to put the file into aQString but that didn't work either:



QString x = trainResult.readAll();

jpn
12th June 2007, 09:08
isn't there anyone who can help me instead of posting links to the docu i've already read?
Did you try following the links? They take you directly to the corresponding examples. Here's one of them:


QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) // <-- you are missing this
return;

QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
process_line(line);
}