How to call qDebug without added spaces and a new line?

I use the print function C ++ / Qt qDebug, but sometimes I would like to control how “space and new line are added and not use qDebug by default.

Take a simple example:

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

Will open

 "some string" = 1 

But let’s say that I don’t like the attached “and space” and would like the print to look like

 some string=1 

How can I call qDebug then?




Note. The qDebug function is called nospace , but it will remove spaces. But "still there."

If I use this:

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

I get:

 "some string"=1 

But keep in mind that I have not yet found a way to get rid of the final new line.

/Thank

+48
c ++ qt qdebug
Mar 06 2018-11-11T00:
source share
7 answers

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

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

+44
Mar 06 '11 at 10:40
source share

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); } // Somewhere in your program qInstallMsgHandler(customHandler); qDebug() << "One" << "Two"; qDebug().noSpace() << "Three" << "Four"; 

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.

+64
Mar 22 '13 at 19:01
source share

Since Qt 5.4 you can also write:

 qDebug().nospace().noquote() << var1; 
+21
Jan 12 '15 at 9:26
source share

Combining some of the answers above, you can use

 qDebug() << qPrintable(var1); 

to remove the surrounding quotation marks.

+17
Feb 11 '13 at 15:07
source share

I also ran into the issue of quotes. The solution is to not stream QString() into the stream, but instead QString(...).toStdString().c_str() .

I built myself a small handy macro to easily get around this:

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

Now every time you use QString, do it like this:

 qDebug() << Q(var1) << "=" << var2; 
+7
Sep 27 '11 at 12:29
source share

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

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

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

+5
Mar 06 '11 at 10:23
source share

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

0
Mar 06 2018-11-11T00:
source share



All Articles