23

I disable the qDebug() messages by writing

CONFIG(release, debug|release):DEFINES += QT_NO_DEBUG_OUTPUT

in the .pro file. This works fine. I would like to know if there is any way to enable qDebug() messages using the source code. I would like to send it as an argument to the application. Therefore by default I will have the qDebug() messages disabled, but with an argument stating to switch on the qDebug() messages I need the qDebug() enabled. Is it possible to do this?

Any help is appreciated

4
  • 2
    You can look at qInstallMsgHandler documentation.
    – vahancho
    Feb 16, 2015 at 11:50
  • @vahancho that line in .pro file not stop console.log for me Jul 6, 2017 at 8:06
  • 1
    @mohammadalabid, why do you add it to .pro file? It should be added to your sorce code. Please see the answer on this question and Qt docs: doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler .
    – vahancho
    Jul 6, 2017 at 8:08
  • i mean this line CONFIG(release, debug|release):DEFINES += QT_NO_DEBUG_OUTPUT Jul 6, 2017 at 8:14

3 Answers 3

17

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.
11

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

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

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.