[SOLVED] -Difference between QTextStream and QString

  • Hi All,

    I am not able to understand why I need to use QTextStream instead of just QString.
    QTextStream filmListTS(&filmListStr);

    In the above filmListStr is a QString. Why should it be copied to QTextStream.

    Thanks again.

  • QTextStream works somewhat like std::stringstream. So you can do something like this:

    QTextStream stream;
    stream << "Adding" << "text" << "and" << "number" << 56 << '\n';

    Here I have just used it as a easy way to append results to an empty string. You could later retrieve a copy of the string with @stream.readAll()@

    You can also use the stream to perform I/O operations on files.

    QFile data("foo.txt");
    if (data.open(QFile::WriteOnly)) {
    QTextStream out(&data);
    out << "Text that is going to be added to the file\n";

    Here I open foo.txt, and add some stuff to it.

    As far as why you have to pass your QString to a QTextStream depends on your application. But note that you pass as a pointer, so operations on filmListTS will reflect on filmListStr.

    QString some_text = "Give me more.";
    QTextStream some_stream(&some_text);
    some_stream << "That's enough!";

    After that, the content of some_text would be "Give me more. That's enough!".

    Have a look at the "documentation":http://qt-project.org/doc/qt-5/QTextStream.html for more details, and possible use cases.

  • Thanks again Leonardo.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.