Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between qDebug() used as a stream and as a function

Tags:

c++

qt

qdebug

I have seen bits of Qt code that uses qDebug as if it were printf()

qDebug( format, ... );

Mostly i see it used like std::cout

qDebug() << "one " << var_one;

What is the difference in the usages and when is it correct/better to use one of the other? The Qt help online somehow seems to reference the function version but never explain it.

like image 762
Wes Miller Avatar asked Apr 02 '14 22:04

Wes Miller


1 Answers

qDebug(pattern, object1, object2) it's basically the old fashioned fprintf(stderr, pattern, object1, object2), as such you depend on compiler support to avoid - for instance - to crash your program with wrong patterns, like int x; qDebug("%s\n", x);. Well, GCC catches this one, but the compiler cannot always know if the pattern is appropriate, I think.

I always use qDebug() << object << ...;, as the documentation states

If you include QtDebug, a more convenient syntax is also available:

qDebug() << "Brush:" << myQBrush << "Other value:" << i;

With this syntax, the function returns a QDebug object that is configured to use the QtDebugMsg message type. It automatically puts a single space between each item, and outputs a newline at the end. It supports many C++ and Qt types.

you can pass most of Qt objects to qDebug() << ... and get them rendered in readable way

try for instance qDebug() << QTime::currentTime();

like image 169
CapelliC Avatar answered Sep 28 '22 00:09

CapelliC