Search code examples
c++qtmacrosqt5qdebug

Disable qDebug output locally with a macro


I used qDebug all over the code. Now I would like to limit its output by translation units, defining a separate macro to enable/disable qDebug output in a translation unit:

test.pro:

DEFINES += NO_DEBUG_ONE

testone.cpp:

#ifdef NO_DEBUG_ONE
#define QT_NO_DEBUG_OUTPUT
#endif

testtwo.cpp:

#ifdef NO_DEBUG_TWO
#define QT_NO_DEBUG_OUTPUT
#endif

So, setting macros like this I expected to get qDebug output only in testtwo.cpp, but I see the qDebug messages from both translation units.

What am I missing here and how to solve it?


Solution

  • Move your code to the top of testone.cpp and testtwo.cpp and you should be good to go.

    You can use QT_NO_DEBUG_OUTPUT and QT_NO_WARNING_OUTPUT when you compile your app but those must be set before the Qt headers are included.

    Using cmake you could add those defines on a per-file base but I don't see a simple way doing that with qmake.