QFile textFile("example.txt"); if (!textFile.open(QFile::ReadOnly | QFile::Text)) { qDebug() << "Failed to open file"; return 1; } QTextStream textStream(&textFile); QString allText = textStream.readAll(); qDebug() << allText;
QString text = "This is an example string."; QTextStream textStream(&text, QIODevice::ReadOnly); QString allText = textStream.readAll(); qDebug() << allText;In this example, a QString containing some text is passed to the QTextStream constructor as the device to read from. The readAll method is then called to read all the text from the QString. The result is printed to the console. QTextStream is part of the Qt Core library.