How to enable and disable qDebug() messages

You can control QDebug (and all messages) at runtime via a few options:

  1. QLoggingCategory Which allows you to use environment variables or config files. For example with Qt 5.6 you can do: QT_LOGGING_RULES="*.debug=false;driver.usb.debug=true" turns on qDebug for everything except USB debug. If you need a more complicated setup, or if you are using Qt 5.5 or earlier you can turn on and off individual debug messages via a qtlogging.ini file.
  2. QT_MESSAGE_PATTERN can also be used to control message output as well as doing formatting.

Adding this code to my qmake project file enabled qDebug() messages for release builds on Windows/MSYS, even without the qInstallMsgHandler():

CONFIG += console
CONFIG += warn_on

You can add your own function for handling Qt debug messages with qInstallMsgHandler

This will allow you to control whether to print the messages.

Tags:

Qt

Qdebug