Since Qt 5.5 you can use QTextStream::readLineInto
. It behaves similar to std::getline
and is maybe faster as QTextStream::readLine
, because it reuses the string:
QIODevice* device;
QTextStream in(&device);
QString line;
while (in.readLineInto(&line)) {
// ...
}
Here's the example from my code. So I will read a text from 1st line to 3rd line using readLine() and then store to array variable and print into textfield using for-loop :
QFile file("file.txt");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
QString line[3] = in.readLine();
for(int i=0; i<3; i++)
{
ui->textEdit->append(line[i]);
}
QFile inputFile(QString("/path/to/file"));
inputFile.open(QIODevice::ReadOnly);
if (!inputFile.isOpen())
return;
QTextStream stream(&inputFile);
QString line = stream.readLine();
while (!line.isNull()) {
/* process information */
line = stream.readLine();
};
Source: Stackoverflow.com