Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to enable and disable qDebug() messages inside a class
Forum Updated to NodeBB v4.3 + New Features

How to enable and disable qDebug() messages inside a class

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 5 Posters 11.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J JonB
    8 Jun 2022, 10:29

    @QtTester
    Now you are getting demanding!

    The effect of #if !defined(QT_NO_DEBUG_OUTPUT) is acted on in qloggingcategory.h (https://code.woboq.org/qt5/qtbase/src/corelib/io/qloggingcategory.h.html#121).

    Since that, like all Qt header files, is inside a #ifndef QLOGGINGCATEGORY_H guard, that file is only read/included the first time that file is included into any particular source file. So switching QT_NO_DEBUG_OUTPUT on & off within one file won't have the effect you seem to want.

    You could presumably achieve the same effect to "scope" the enablement/disablement with something based on:

    // Next line at *beginning* of your header file
    #undef qCDebug
    #  define qCDebug(category, ...) QT_NO_QDEBUG_MACRO()
    
    ...
    
    // Next line at *end* of your header file
    #undef qCDebug
    #  define qCDebug(category, ...) \
        for (bool qt_category_enabled = category().isDebugEnabled(); qt_category_enabled; qt_category_enabled = false) \
            QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).debug(__VA_ARGS__)
    

    but it's getting messy, and relies on knowing what the definition of qCDebug is in Qt, which could change.

    Better would be to write something other than qDebug() for whichever things you want to enable/disable, or use a dedicated category you define for those lines.

    Q Offline
    Q Offline
    QtTester
    wrote on 8 Jun 2022, 11:56 last edited by
    #21

    @JonB
    we discuss and you give solution, others will be benefited, tripartite win. :-)

    1 Reply Last reply
    0

    21/21

    8 Jun 2022, 11:56

    • Login

    • Login or register to search.
    21 out of 21
    • First post
      21/21
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved