Read textfile and display on QLabel [SOLVED]
-
So I am trying to read a textfile and display it on a QLabel. This is what I have:
QFile file("test.txt"); QLabel *testLabel= new QLabel; QString line; if (file.open(QIODevice::ReadOnly | QIODevice::Text)){ QTextStream stream(&file); while (!stream.atEnd()){ line = stream.readLine(); testLabel->setText(line+"\n"); } } file.close();
However, it only writes the last line of the file. So if my file is:
apple
oranges
bananait only displays banana.
-
Thank you!! I changed it to the following and it works!!!!
QFile file("test.txt"); QLabel *testLabel= new QLabel; QString line; if (file.open(QIODevice::ReadOnly | QIODevice::Text)){ QTextStream stream(&file); while (!stream.atEnd()){ line.append(stream.readLine()+"\n"); } testLabel->setText(line); } file.close();