I find << and >> operators convenient.
You don't "convert" a stream to a string, you read from a stream to a string.
Following example demonstrates usage of QTextStream and QStringList:
// writing strings to a stream
textStream << string1 << " " << string2;
// stream's internal data
qDebug() << data; // outputs "string 1 string 2"
// reading from a stream to a string list
while (!textStream.atEnd())
{
textStream >> word;
wordList << word;
}
qDebug() << wordList; // outputs ("string", "1", "string", "2")
// writing a string list to a stream
textStream << word;
qDebug() << textStream.readAll(); // outputs "string1string2"
QString string1("string 1");
QString string2("string 2");
QString data;
QTextStream textStream(&data);
// writing strings to a stream
textStream << string1 << " " << string2;
// stream's internal data
qDebug() << data; // outputs "string 1 string 2"
// reading from a stream to a string list
QStringList wordList;
while (!textStream.atEnd())
{
QString word;
textStream >> word;
wordList << word;
}
qDebug() << wordList; // outputs ("string", "1", "string", "2")
// writing a string list to a stream
foreach (QString word, wordList)
textStream << word;
qDebug() << textStream.readAll(); // outputs "string1string2"
To copy to clipboard, switch view to plain text mode
Hope you find it useful! And if not, please give a bit more comprehensive explanation of what you are trying to do..
Bookmarks