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. output QString
Forum Updated to NodeBB v4.3 + New Features

output QString

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 5.1k 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.
  • W Offline
    W Offline
    Weichao Wang
    wrote on last edited by
    #1

    Dear all,
    For QString str,
    cout << str;
    produces error while compiling, and one must use str.toStdString(), but
    QTextStream << str.toStdString();
    produces error, and one must use str (without .toStdString()). What's the reason that Qt is designed this way? Is it not possible to design QString this way so that cout and QTextStream would have the same syntax?
    Weichao

    aha_1980A jsulmJ JKSHJ 3 Replies Last reply
    0
    • W Weichao Wang

      Dear all,
      For QString str,
      cout << str;
      produces error while compiling, and one must use str.toStdString(), but
      QTextStream << str.toStdString();
      produces error, and one must use str (without .toStdString()). What's the reason that Qt is designed this way? Is it not possible to design QString this way so that cout and QTextStream would have the same syntax?
      Weichao

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Weichao-Wang

      Try this:

      QTextStream output(stdout);
      QString str = "Hello World";
      
      output << str;
      

      Qt has to stay free or it will die.

      W 1 Reply Last reply
      2
      • aha_1980A aha_1980

        @Weichao-Wang

        Try this:

        QTextStream output(stdout);
        QString str = "Hello World";
        
        output << str;
        
        W Offline
        W Offline
        Weichao Wang
        wrote on last edited by
        #3

        @aha_1980
        You mean that this way we can avoid using cout so that file and console output would behave the same way?
        Weichao

        1 Reply Last reply
        0
        • W Weichao Wang

          Dear all,
          For QString str,
          cout << str;
          produces error while compiling, and one must use str.toStdString(), but
          QTextStream << str.toStdString();
          produces error, and one must use str (without .toStdString()). What's the reason that Qt is designed this way? Is it not possible to design QString this way so that cout and QTextStream would have the same syntax?
          Weichao

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Weichao-Wang said in output QString:

          Is it not possible to design QString this way so that cout and QTextStream would have the same syntax?

          You can implement this:

          std::ostream&  operator <<(std::ostream &stream,const QString &str)
          {
             stream << str.toAscii().constData(); //or: stream << str.toStdString(); //??
             return stream;
          }
          

          See https://stackoverflow.com/questions/16281014/operator-for-qstring

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          aha_1980A 1 Reply Last reply
          3
          • W Offline
            W Offline
            Weichao Wang
            wrote on last edited by
            #5

            Thank you all!
            Weichao

            1 Reply Last reply
            0
            • jsulmJ jsulm

              @Weichao-Wang said in output QString:

              Is it not possible to design QString this way so that cout and QTextStream would have the same syntax?

              You can implement this:

              std::ostream&  operator <<(std::ostream &stream,const QString &str)
              {
                 stream << str.toAscii().constData(); //or: stream << str.toStdString(); //??
                 return stream;
              }
              

              See https://stackoverflow.com/questions/16281014/operator-for-qstring

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @jsulm said in output QString:

              stream << str.toAscii().constData();

              toAscii() is deprecated, use toLatin1() instead.

              @Weichao-Wang

              However, note that QString is an Unicode string, using toLatin1() will only work for letters in the Latin-1 encoding range. Most likely toUtf8() or toLocal8Bit() will be the better solution.

              Qt has to stay free or it will die.

              jsulmJ 1 Reply Last reply
              3
              • aha_1980A aha_1980

                @jsulm said in output QString:

                stream << str.toAscii().constData();

                toAscii() is deprecated, use toLatin1() instead.

                @Weichao-Wang

                However, note that QString is an Unicode string, using toLatin1() will only work for letters in the Latin-1 encoding range. Most likely toUtf8() or toLocal8Bit() will be the better solution.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @aha_1980 I just copy pasted the code from the link above :-) I did this just to show the direction. But yes, because people often just copy paste code from forum it is good you corrected the issues in the code!

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • W Weichao Wang

                  Dear all,
                  For QString str,
                  cout << str;
                  produces error while compiling, and one must use str.toStdString(), but
                  QTextStream << str.toStdString();
                  produces error, and one must use str (without .toStdString()). What's the reason that Qt is designed this way? Is it not possible to design QString this way so that cout and QTextStream would have the same syntax?
                  Weichao

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  @Weichao-Wang said in output QString:

                  What's the reason that Qt is designed this way?

                  std::string has a terrible design. I am glad that QString is designed differently, because QString is much more powerful and easy to use.

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  1

                  • Login

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