Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a QString from a QTextStream?

Tags:

qt

qstring

Will this work?

QString bozo;
QFile filevar("sometextfile.txt");

QTextStream in(&filevar);

while(!in.atEnd()) {
QString line = in.readLine();    
bozo = bozo +  line;  

}

filevar.close();

Will bozo be the entirety of sometextfile.txt?

like image 246
Dave Avatar asked Apr 05 '13 00:04

Dave


1 Answers

Why even read line by line? You could optimize it a little more and reduce unnecessary re-allocations of the string as you add lines to it:

QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
QTextStream in(&file);
QString text;    
text = in.readAll();
file.close();
like image 193
dtech Avatar answered Oct 14 '22 09:10

dtech