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. Double quotes in QString
Qt 6.11 is out! See what's new in the release blog

Double quotes in QString

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 15.7k Views 2 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.
  • cpperC Offline
    cpperC Offline
    cpper
    wrote on last edited by cpper
    #1

    The standard way of displaying double quotes in a std::string is by doing string str("\""); . However I see this doesn't work with QStrings:

        QString str("a\"bc\"");
        qDebug()<<str;
        qDebug()<<"a\"bc\"";
    

    The first qDebug prints a\"bc\" , while the second works and prints a"bc" . Any ideas ?

    kshegunovK 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      it's not QString, it's qDebug() see https://bugreports.qt.io/browse/QTBUG-48517 for example with Mr. Knoll himself answering

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      6
      • cpperC cpper

        The standard way of displaying double quotes in a std::string is by doing string str("\""); . However I see this doesn't work with QStrings:

            QString str("a\"bc\"");
            qDebug()<<str;
            qDebug()<<"a\"bc\"";
        

        The first qDebug prints a\"bc\" , while the second works and prints a"bc" . Any ideas ?

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @cpper Of course it works, this is how C++ does it and has nothing to do with Qt. What you're observing is expected, as qDebug() will escape any non-alphanum characters (including non-printables).

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        3
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          the intention for this behavior was to make it easier to debug what's inside QStrings.
          If you want to display the contents you need to do qDebug() << QString(...).toUtf8().constData(), what's actually your second approach.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          kshegunovK 1 Reply Last reply
          2
          • raven-worxR raven-worx

            the intention for this behavior was to make it easier to debug what's inside QStrings.
            If you want to display the contents you need to do qDebug() << QString(...).toUtf8().constData(), what's actually your second approach.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @raven-worx said in Double quotes in QString:

            If you want to display the contents you need to do qDebug() << QString(...).toUtf8().constData()

            Or what I usually suggest:

            QTextStream out(stdout);
            out << str;
            

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            6
            • cpperC Offline
              cpperC Offline
              cpper
              wrote on last edited by
              #6

              All clear now. Thanks guys :)

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Yet another option, cause you can never have too much ;) :

                qDebug().noquote() << str;
                
                1 Reply Last reply
                4

                • Login

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