3

I am using Qt framework in Linux and a complete beginner.

When I print a simple message like:

qDebug() << "Hello World";

In the Console the output is Hello World.

But if I print the same message like:

QString str = "Hello World";
qDebug() << str;

In the Console the output is "Hello World",(Notice the quotes), How to get the same output using QString?

Sumeet
  • 8,086
  • 3
  • 25
  • 45
  • 6
    Possible Duplicate: http://stackoverflow.com/questions/27976581/why-is-qstring-printed-with-quotation-marks – Hayt Sep 16 '16 at 09:04
  • 1
    This is the way QString is implemented. If you want to remove the "", consider using a std::string, or potentially use a QByteArray. – Babra Cunningham Sep 16 '16 at 09:06

2 Answers2

4

See QDebug::noquote

Disables automatic insertion of quotation characters around QChar, QString and QByteArray contents and returns a reference to the stream.

When quoting is disabled, these types are printed without quotation characters and without escaping of non-printable characters.

This function was introduced in Qt 5.4.

Usage:

QString str = "Hello World";
qDebug().noquote() << str;

http://doc.qt.io/qt-5/qdebug.html#noquote

Community
  • 1
  • 1
RvdK
  • 19,580
  • 4
  • 64
  • 107
1

you can find the answer in the Qt source code(detail of implementation in the link below) : https://code.woboq.org/qt5/qtbase/src/corelib/io/qdebug.h.html

you have several definitions of the << operator

from Qt source code :

/*! \fn QDebug &QDebug::operator<<(const char *s)

Writes the '\0'-terminated string s, to the stream and returns a reference to the stream. The string is never quoted nor transformed to the output, but note that some QDebug backends might not be 8-bit clean.

/ /! \fn QDebug &QDebug::operator<<(const QString &s)

Writes the string, \a s, to the stream and returns a reference to the stream. Normally, QDebug prints the string inside quotes and transforms non-printable characters to their Unicode values (\u1234). To print non-printable characters without transformation, enable the noquote() functionality. Note that some QDebug backends might not be 8-bit clean. Output examples: \code QString s; s = "a"; qDebug().noquote() << s; // prints: a qDebug() << s; // prints: "a" s = "\"a\r\n\""; qDebug() << s; // prints: "\"a\r\n\"" s = "\033"; // escape character qDebug() << s; // prints: "\u001B" s = "\u00AD"; // SOFT HYPHEN qDebug() << s; // prints: "\u00AD" s = "\u00E1"; // LATIN SMALL LETTER A WITH ACUTE qDebug() << s; // prints: "á" s = "a\u0301"; // "a" followed by COMBINING ACUTE ACCENT qDebug() << s; // prints: "aÌ"; s = "\u0430\u0301"; // CYRILLIC SMALL LETTER A followed by COMBINING ACUTE ACCENT qDebug() << s; // prints: "аÌ" \endcode */

qDebug()<< "hello world" uses QDebug &QDebug::operator<<(const char *s) and not QDebug &QDebug::operator<<(const QString &s) that's why you get the quotes in one version and not the other. you can get the same result by using :

 qDebug().noquote() << s;

on the QString version

Venom
  • 1,010
  • 10
  • 20
  • The code you've posted is not source code of Qt, it is from the [docs](https://doc.qt.io/qt-5/qdebug.html#operator-lt-lt-13). The answer should contain a link (and an explanation of the options) rather than the comments in the source code of Qt. – Mike Sep 16 '16 at 09:16
  • 1
    You forgot to add the solution, you only showed the explanation. – RvdK Sep 16 '16 at 09:33
  • the solution is at the end! qDebug().noquote() << s; – Venom Sep 16 '16 at 09:43