Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] -Difference between QTextStream and QString
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] -Difference between QTextStream and QString

Scheduled Pinned Locked Moved General and Desktop
3 Posts 1 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paawan
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • ? This user is from outside of this forum
      ? This user is from outside of this forum
      Guest
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Paawan
        wrote on last edited by
        #3

        Thanks again Leonardo.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved