Double quotes in QString



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



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


  • Qt Champions 2016

    @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).


  • Moderators

    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.


  • Qt Champions 2016

    @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;
    


  • All clear now. Thanks guys :)


  • Moderators

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

    qDebug().noquote() << str;
    

Log in to reply
 

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