It’s best to understand how QDebug works internally. This way you can easily change it to suit your needs. Whenever you use the qDebug() function, it returns a QDebug object. By default, QDebug always displays a space after using operator << .
Inside the QDebug class, there is a QString . Each time you use operator << , you add to this 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 a line followed by a new line.
Normal output
qDebug() << "Var" << 1;
Var 1 will be displayed. This is because QDebug will create a QDebug object that adds a space after each call to operator << . So it will be Var + + 1 + .
Without spaces
You can use QDebug::nospace to tell QDebug not to add a space after each call to operator << .
qDebug().nospace() << "Var" << 1;
This will lead to the output of Var1 , since this QDebug object no longer prints spaces.
No new lines
Not adding \n to the end of the line is a little harder. Since QDebug internally passes the qt_message_output string only when it is destroyed, you can delay the destruction of this QDebug object -
QDebug deb = qDebug(); deb << "One" << "Two"; deb << "Three";
This will print One Two Three and then add a new line.
If you never want a new line to be printed, you will have to change the behavior 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); }
One Two ThreeFour .
Be warned that this will affect all qDebug instructions in your program. If you want to remove the custom handler, you must call qInstallMsgHandler(0) .
qDebug (const char * msg, ...)
As pointed out in other answers, you can also use the QDebug function to print lines in a format similar to printf format. This way you can avoid the extra spaces added by QDebug .
However, QDebug internally still uses qt_message_output , so you still get a new line at the end if you don't install your own handler.