升级Ubuntu 17.10/Fedora后qDebug()无控制台输出,如何重新启用?
Hey there! I've run into this exact issue before after upgrading my Ubuntu system, so I know how frustrating it can be when qDebug() goes silent mid-debugging. Let's break down the most common causes and fixes to get your debug output back:
1. Override Qt's Default Logging Rules
Newer Qt versions shipped with these OS updates often use stricter default logging filters that suppress qDebug() output. You can quickly test this by setting an environment variable before running your app:
export QT_LOGGING_RULES="*.debug=true" ./your-qt-application
If this works, make it permanent by adding that export line to your ~/.bashrc (for Bash) or ~/.zshrc (for Zsh). For GUI apps launched from the desktop, you can add the variable to your app's .desktop file under the Exec line:
Exec=env QT_LOGGING_RULES="*.debug=true" /path/to/your/app
Alternatively, set the rules directly in your code early on (before any qDebug() calls):
#include <QLoggingCategory> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QLoggingCategory::setFilterRules("*.debug=true"); // Enable all debug messages QApplication app(argc, argv); // ... rest of your code }
2. Verify Build Configuration Isn't Disabling Debug Output
If you're building in Release mode, Qt automatically disables qDebug() unless you explicitly enable it. Open your project's .pro file and:
- Ensure there's no
DEFINES += QT_NO_DEBUG_OUTPUTline (this completely turns offqDebug()). - Add
DEFINES += QT_MESSAGELOGCONTEXTif you want to include file/line numbers in debug output (optional but super helpful for tracing issues).
After making changes, re-run qmake and rebuild your project.
3. Run Your App Directly in the Terminal
Desktop environments like GNOME on Ubuntu/Fedora often redirect stdout/stderr of GUI apps away from the terminal. Instead of launching from the desktop menu, open a terminal, navigate to your app's directory, and run it directly. This ensures you'll see any output being sent to the console.
4. Check System-Wide Qt Logging Settings
Some OS updates modify the global Qt logging config file. Check /etc/xdg/QtProject/qtlogging.ini for lines like:
[Rules] *.debug=false
If you find this, either delete the line or change false to true. You'll need sudo privileges to edit the file:
sudo nano /etc/xdg/QtProject/qtlogging.ini
Save the changes and restart your app to apply them.
Bonus: Isolate the Issue with a Minimal Test App
If none of the above works, create a tiny test app to rule out project-specific issues:
#include <QApplication> #include <QDebug> int main(int argc, char *argv[]) { QApplication app(argc, argv); qDebug() << "Hello from qDebug! Is this working?"; return app.exec(); }
Build and run this. If it prints, the problem is in your main project's configuration; if not, it's a system-wide Qt setting that needs adjustment.
内容的提问来源于stack exchange,提问作者Yusef Maali

