66

I'm using the the C++/Qt print function qDebug, but sometimes I would like to control how ", space and newline is appended and not use the default qDebug.

Let's take a simple example:

QString var1("some string");
int var2 = 1;
qDebug() << var1 << "=" << var2;

This will print

"some string" = 1 

But Let's say that I don't like the appended " and space and would like the print to look like

some string=1 

How to I then call qDebug?


Note: There is a function in qDebug called nospace, but it will remove the spaces. But the " is still there.

If I use this:

qDebug().nospace() << var1 << "=" << var2;

I get:

"some string"=1

But please note that I have still not found a way to get rid of the ending newline.

/Thanks

leyyin
  • 27
  • 1
  • 4
Johan
  • 20,067
  • 28
  • 92
  • 110

8 Answers8

103

It would be best to understand how QDebug works internally. That way you can easily modify it to suit your needs. Whenever you use the qDebug() function, it returns a QDebug object. By default QDebug always outputs a space after any use of operator <<.

The QDebug class internally contains a QString. Every time you use operator << you are appending to that internal QString. This QString is printed via qt_message_output(QtMsgType, char*) when the QDebug object is destroyed.

By default qt_message_output always prints the string followed by a newline.

Normal Output

qDebug() << "Var" << 1;

This will output Var 1. This is because qDebug will create a QDebug object which appends a space after each call to operator <<. So that will be Var + + 1 + .

Without Spaces

You can use QDebug::nospace to tell QDebug not to append a space after each call to operator <<.

qDebug().nospace() << "Var" << 1;

This will output Var1 as that QDebug object is no longer printing spaces.

Without New Lines

Not adding the \n at the end of the string is a little bit harder. Since QDebug internally only passes the string to qt_message_output when it is destroyed, you can delay the destruction of that QDebug object -

QDebug deb = qDebug();
deb << "One" << "Two";
deb << "Three";

This will print One Two Three and then append a new line.

If you never want a new line to be printed, you will have to change the behaviour of qt_message_output. This can be done by installing a custom handler.

void customHandler(QtMsgType type, const char* msg) {
    fprintf(stderr, msg);
    fflush(stderr);
}

// Somewhere in your program
qInstallMsgHandler(customHandler);

qDebug() << "One" << "Two";
qDebug().noSpace() << "Three" << "Four";

This will print One Two ThreeFour.

Be warned that this will affect all of the qDebug statements in your program. If you want to remove the custom handler, you should call qInstallMsgHandler(0).

qDebug(const char* msg, ...)

As indicated by the other answers you can also use the qDebug function to print strings in a format similar to that of printf. This way you can avoid the extra spaces that are appended by QDebug.

However, qDebug internally still uses qt_message_output, so you will still get a newline at the end unless you install your own handler.

Vishesh Handa
  • 1,830
  • 2
  • 14
  • 17
55

Try this format: qDebug("%s=%d", "string", 1); In this case qDebug uses printf formatting

P.S. Adapted for your example: qDebug("%s=%d", var1.toStdString().c_str(), var2);

Johnny
  • 1,966
  • 17
  • 24
  • 23
    Instead of `var1.toStdString().c_str()` I'd recommend `qPrintable( var1 )`. Both work fine, but `qPrintable()` looks nicer. :) – Robin Mar 11 '11 at 14:17
  • 2
    If you do that, `qUtf8Printable()` is what should be used, according to the [docs](http://doc.qt.io/qt-5/qtglobal.html#qPrintable), as `qDebug` and the other Qt logging functions expect `%s` arguments to be UTF-8. – ePirat Dec 23 '16 at 23:59
  • 5
    This still adds a new line, though! – Violet Giraffe Feb 17 '20 at 11:25
29

Since Qt 5.4 you can also write:

qDebug().nospace().noquote() << var1;
user4444617
  • 299
  • 3
  • 2
  • 2
    How can I set this globally? – ManuelSchneid3r Jan 04 '17 at 22:09
  • @ManuelSchneid3r you can't, but you can hack it by `#undef qDebug` and the redefining as `#define qDebug QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).debug().noquote().nospace` (or just create your own macro). The problem here is that you have to make this new macro globally accessible and won't cross executable frontiers (as to another shared library also using `qDebug`) – cbuchart Jan 11 '19 at 15:03
  • It is strange, why the heck, does QDebug adds something to the strings, and we need explicitly disable them... When I write qDebug() << str, i expect it to print the string exactly as it is, without adding quotes, spaces or whathever else. – Nuclear Dec 29 '21 at 19:04
19

Combining some of the above answers you can use

qDebug() << qPrintable(var1);

to eliminate the surrounding quotes.

Tim MB
  • 4,413
  • 4
  • 38
  • 48
7

I also experienced the quotes problem. The solution is to not pipe QString() into the stream but instead QString(...).toStdString().c_str().

I've built myself a small convenience macro to easily get around this:

#define Q(string) (string).toStdString().c_str()

Now everytime you use a QString, do it like that:

qDebug() << Q(var1) << "=" << var2;
hurikhan77
  • 5,881
  • 3
  • 32
  • 47
4

The file $(QTDIR)/src/corelib/io/qdebug.h contains almost all definitions for the debug output methods. One of them is:

inline QDebug &operator<<(const QString & t) { stream->ts << '\"' << t << '\"'; return maybeSpace(); }

So there is no "official" way to suppress the quotes, but you can of course change qdebug.h or use your own copy or a modified and renamed copy of the QDebug class.

hmuelner
  • 8,093
  • 1
  • 28
  • 39
2

Instantiate a QDebug object and output to it:

QDebug dbg = qDebug().nospace().noquote();

dbg << var1 << "=" << var2;

Yields:

some string=1

Output to the dbg object all you want -- there won't be a newline until it goes out of scope. For example:

char var1[] = "some string";
int var2 = 1;

{
    QDebug dbg = qDebug().nospace().noquote();

    dbg << var1 << "=" << var2;

    // keep using "dbg"; there's no newline ('\n') until it destructs
    dbg << "...";
    for (int i = 5; i <=9; ++i)
        dbg << i;
}

Outputs:

some string=1...56789
pixelgrease
  • 1,940
  • 23
  • 26
0

Another way is to use your own message handler.
Hope this helps.

AkiRoss
  • 11,745
  • 6
  • 59
  • 86
zkunov
  • 3,362
  • 1
  • 20
  • 17
  • I am not sure, but from the documentation it does not seems *appropriate* to change the formatting from the message handler. – AkiRoss Apr 30 '13 at 00:53
  • when to break the rules is something only professionals knows, if this was a fatal action or never appropriate they wouldn't make it possible in the first place – zkunov Jun 21 '13 at 20:49
  • Well, I absolutely do not share that point of view... Meaning you can does mean it is right or was intended to. – AkiRoss Jun 21 '13 at 21:32
  • example of appropriate usage here: http://stackoverflow.com/questions/4954140/how-to-redirect-qdebug-qwarning-qcritical-etc-output – zkunov Jun 22 '13 at 22:53
  • 1
    btw, just realizing if what you're trying to say is that using own message handler is too much just to remove the spaces and new line, I guess it depends from the code base, how many times you're going to use the new formating and so on, but it is a valid option that no one had mentioned when I was writing this – zkunov Jun 22 '13 at 23:08