Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Silly? problem with QString
Qt 6.12 LTS is out! See what's new in the release blog

Silly? problem with QString

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 14.2k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi!

    I'm just started work with Qt maybe week ago. I'm learning cpp for four years so I hope the adventure with Qt will be quite exciting ;)

    I tried to search in many places, but still I have silly problem with QString in console application.

    I'm trying to write QString on standard output with using cout

    @
    QString txt = "blabla";
    cout << txt;
    @

    but it will not compile:

    @
    no match for 'operator <<' in 'std::cout << txt'
    @

    But for me it's weird, because I found (in documentation), that << operator is overloaded in QString. And, as far as I know, when we overload operator in class we can use is "normally". As if it was in built-in type.

    I found solution:

    @
    QString txt = "blabla";

    string txt2 = qstrdup(txt.toLatin1());
    
    cout << txt;
    

    @

    But it's not convincing me. Is there quicker way to write QString in console? For example some write function (I mean: txt2.write(), or write(txt))

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Many Qt developers using qDebug(), but you can use this if you need std::cout:
      @
      cout << qPrintable(txt);
      @

      [quote author="luka.v.pikor" date="1278592314"]But it&#39;s not convincing me. Is there quicker way to write QString in console? For example some write function (I mean: txt2.write(), or write(txt))
      [/quote]

      @
      QTextStream(stdout) << txt;
      @

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        P.S.: need feature for remove or/and join posts
        P.S.: questions like that need a posting to "Desktop":http://developer.qt.nokia.com/forums/viewforum/10/ section

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @
          QTextStream(stdout) << txt;
          @

          works perfectly! Thanks!

          But what is qPrintable? I can't find it(http://doc.qt.nokia.com/4.6/classes.html) in opposite to qDebug().

          Also, I have the same problem with QDate.

          @
          QDate d1(2010,1,1);
          @

          The TextStream didn't work. I assume that I have to use similar function. Or maybe convert Date type to string format?

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DenisKormalev
            wrote on last edited by
            #5

            [quote author="luka.v.pikor" date="1278593533"]@
            Also, I have the same problem with QDate.
            @
            QDate d1(2010,1,1);
            @
            The TextStream didn&#39;t work. I assume that I have to use similar function. Or maybe convert Date type to string format?
            [/quote]

            Use QDate::toString() methods firstly.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              [quote author="luka.v.pikor" date="1278593533"]
              But what is qPrintable? I can&#39;t find it(http://doc.qt.nokia.com/4.6/classes.html) in opposite to qDebug().
              [/quote]
              You can found this function in QtGlobal module, it's equivalent

              @str.toLocal8Bit().constData()@

              [quote author="luka.v.pikor" date="1278593533"]
              Also, I have the same problem with QDate.
              The TextStream didn&#39;t work.
              [/quote]
              For debugging purpose you can use qDebug(), this function have overloaded operator "<<" for many Qt classes.
              @qDebug() << QDate(2010,1,1);@

              [quote author="luka.v.pikor" date="1278593533"]
              I assume that I have to use similar function. Or maybe convert Date type to string format?
              [/quote]

              Yes, you must use method toString() for this:

              @QTextStream(stdout) << QDate(2010,1,1).toString();@

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                Thanks Denis and Sabrog for helping me. Now it works!

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  [quote author="SABROG" date="1278592777"]P.S.: need feature for remove or/and join posts
                  P.S.: questions like that need a posting to "Desktop":http://developer.qt.nokia.com/forums/viewforum/10/ section[/quote]

                  Thanks, I've moved it to Desktop now. A moderator can move, merge, split, and delete - but we haven't really begun finding moderators.

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    [quote author="MariusG" date="1278595484"]A moderator can move, merge, split, and delete
                    [/quote]

                    I think users must have feature for delete own post. About merging, if user post message, forum engine must check, if previous post author in current thread a same, then just auto merge posts if elapsed less than 5-10 minutes. If future merged post have more than character forum limit size, then not merge.

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      ixSci
                      wrote on last edited by
                      #10

                      [quote author="luka.v.pikor" date="1278592314"]Hi!
                      And, as far as I know, when we overload operator in class we can use is "normally". As if it was in built-in type.
                      [/quote]
                      It's not actually right. You have to have global <<(>>) operator when one of the argument is istream\ostream and second is your type. QString doesn't have this operator but feel free to create your own for the convenient using. @std::ostream& operator<<(std::ostream& xOut, const QString& xStr)
                      {
                      xOut << xStr.toStdString();
                      return xOut;
                      }@
                      Also, in addition to answers above I see another solution: QString::toStdString would be appropriate function for your purpose

                      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
                      • Unsolved