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
Combining some of the above answers you can use
to eliminate the surrounding quotes.
Try this format:
qDebug("%s=%d", "string", 1);
In this caseqDebug
uses printf formattingP.S. Adapted for your example:
qDebug("%s=%d", var1.toStdString().c_str(), var2);
It would be best to understand how
QDebug
works internally. That way you can easily modify it to suit your needs. Whenever you use theqDebug()
function, it returns aQDebug
object. By defaultQDebug
always outputs a space after any use ofoperator <<
.The
QDebug
class internally contains aQString
. Every time you useoperator <<
you are appending to that internal QString. This QString is printed viaqt_message_output(QtMsgType, char*)
when theQDebug
object is destroyed.By default
qt_message_output
always prints the string followed by a newline.Normal Output
This will output
Var 1
. This is becauseqDebug
will create aQDebug
object which appends a space after each call tooperator <<
. So that will beVar
++ 1 +
.
Without Spaces
You can use
QDebug::nospace
to tellQDebug
not to append a space after each call tooperator <<
.This will output
Var1
as thatQDebug
object is no longer printing spaces.Without New Lines
Not adding the
\n
at the end of the string is a little bit harder. SinceQDebug
internally only passes the string toqt_message_output
when it is destroyed, you can delay the destruction of that QDebug object -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.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 ofprintf
. This way you can avoid the extra spaces that are appended byQDebug
.However,
qDebug
internally still usesqt_message_output
, so you will still get a newline at the end unless you install your own handler.Another way is to use your own message handler.
Hope this helps.
Since Qt 5.4 you can also write:
I also experienced the quotes problem. The solution is to not pipe
QString()
into the stream but insteadQString(...).toStdString().c_str()
.I've built myself a small convenience macro to easily get around this:
Now everytime you use a QString, do it like that: