PDA

View Full Version : qDebug Problem



Ini
17th April 2016, 18:38
QString a = "test\n";
qDebug() << a;

output of programm is "test\n"

Why it does not interptret correctly? I cannot write after every QString varaible with a \n in it toLatin1().data() to get an correct output.

Is there no simple solution in Qt?

thank you

d_stranz
17th April 2016, 22:37
qDebug() is doing exactly what you told it to: print the value of the QString variable "a". The value of "a" is a string composed of the letters 't', 'e', 's', 't', '\n'.

Think about what you are asking for - if qDebug() interpreted non-printing characters like '\t' or '\n', how would you ever be able to print the actual contents of a string that contained those? It's a tool to aid you in debugging, where you can ask it to print the actual contents of built-in types, Qt types, and custom types for which you have implemented the right operator<<().

qDebug() outputs types with default formatting. If you want qDebug to custom format your output, you can use QTextStream manipulators. qDebug() automatically inserts a newline after ever statement and inserts spaces between arguments separated by <<.

Ini
19th April 2016, 12:55
"qDebug() outputs types with default formatting. If you want qDebug to custom format your output, you can use QTextStream manipulators."

Can you explain pls?

d_stranz
20th April 2016, 16:22
It is explained in the QDebug and QTextStream documentation. Look for the section on manipulators in the QTextStream docs.

Try



qDebug() << qSetFieldWidth( 8 ) << qSetRealNumberPrecision( 2 ) << 3.14159;
qDebug() << fixed << right << 3.14159;

Ini
20th April 2016, 17:14
thank you_____

d_stranz
20th April 2016, 17:25
You could also do simpler things like:



qDebug() << QString( "%1" ).arg( 3.14159, 8, 'f', 2 );


which will produce the same output as the first line in the previous reply.