How can I read an entire UTF-8 encoded file to a QString in Qt, and is there a one-liner similar to C#'s string contents = File.ReadAllText(@"C:\temp\test.txt");
Asked
Active
Viewed 404 times
1 Answers
0
The shortest I got to was this:
QFile f("foo.txt");
f.open(QFile::ReadOnly);
QString s = QString::fromUtf8(f.readAll());
I was hoping to use something like QString::fromUtf8(QFile("foo.txt").readAll())
which is a one-liner albeit clunky, but the constructor doesn't open the file, so you need to declare a variable and the shortest is 3 lines.

sashoalm
- 75,001
- 122
- 434
- 781