解决办法

环境:

qt5-qtbase-devel 5.13.2 @ Fedora 32 Workstation Edition

qt5-qtbase-devel 5.11.1 @ CentOS Linux release 8.1.1911

首先检查CONFIG是不是包含console, 如果没有则加上 CONFIG += console

检查$HOME/.config/QtProject/qtlogging.ini文件(如果没有则创建)是否包含以下内容:

    [Rules]
    *.debug=true
    qt.*.debug=false

重新运行Qt程序,就可以看到qDebug的输出了。

如果需要变成全局配置,可以修改文件/etc/xdg/QtProject/qtlogging.ini(没有则自行创建)。

原因分析

这个问题可能是RHEL系里独有的, 至少我以前在ArchLinux下调试 qt5 程序时没有遇到过这个问题。

有一个bug报告是跟这个问题相关的 https://bugzilla.redhat.com/show_bug.cgi?id=1227295

事实上本文上面的解决办法来自 第10条评论

第15楼也很有料,根据他提供的 链接: http://pkgs.fedoraproject.org/cgit/rpms/qt5-qtbase.git/commit/?id=a09edb40e5587bd34d73aaec637d89d049ebbb0e 老灯发现,这个commit里,Fedora的qt5-qtbase包默认ship了一个配置%{_qt5_datadir}/qtlogging.ini, 其中包含一条禁用debug的规则(*.debug=false),并带上了注释:

distro defaults, see also https://bugzilla.redhat.com/show_bug.cgi?id=1227295 can override in $XDG_CONFIG_HOME/QtProject/qtlogging.ini or $XDG_CONFIG_DIR/QtProject/qtlogging.ini

即一般情况下,可通过$HOME/.config/QtProject/qtlogging.ini/etc/xdg/QtProject/qtlogging.ini 来override这个配置。

29楼同样很优秀啊,给出一个非常实用的tips

QT_LOGGING_DEBUG=1 causes loadRulesFromFile to show files loaded

尝试一下, 发现果然显示了这些rules来自哪些文件:

export QT_LOGGING_DEBUG=1

export QT_MESSAGE_PATTERN='%{function}: %{message}'

❯ ./dukto
loadRulesFromFile: Loading "/usr/share/qt5/qtlogging.ini" ...
loadRulesFromFile: Loading "/etc/xdg/QtProject/qtlogging.ini" ...
main: os: linux
main: setActivationWindow
main: showExpanded
main: installEventFilter
GuiBehind::eventFilter: GuiBehind::eventFilter(): application activate sayHello
GuiBehind::close: application activate sayGoodbye

从上面可看到,在Fedora 32下, qt5 默认先加载/usr/share/qt5/qtlogging.ini, 我们看看这个文件内容:

❯ cat /usr/share/qt5/qtlogging.ini
[Rules]
*.debug=false
qt.qpa.xcb.xcberror.warning=false

不但禁用了所有debug日志,还同时屏蔽了xcb QPA (Qt Platform Abstraction) platform plugin warning信息.

什么是xcb? https://doc.qt.io/qt-5/linux-requirements.html

On Linux, the xcb QPA (Qt Platform Abstraction) platform plugin is used. It provides the basic functionality needed by Qt GUI and Qt Widgets to run against X11. Its library dependencies are described the following table. To build Qt from its source code, you will also need to install the development packages for these libraries for your system.

注意xcb是只针对X11的,如果是Wayland 情况又不同了。需要 QtWayland

What is QtWayland? QtWayland is a Qt 5 module that wraps the functionality of Wayland. QtWayland is separated into a client and server side. The client side is the wayland platform plugin, and provides a way to run Qt applications as Wayland clients. The server side is the Qt Wayland Compositor API, and allows users to write their own Wayland compositors.

老灯之所以还在用X11, 完全是因为NVIDIA显卡的官方驱动目前对于Wayland的支持不好。

如果查看下udev规则,就会发现Fedora 32目前为止都是默认为gdm禁用了Wayland:

❯ bat /usr/lib/udev/rules.d/61-gdm.rules
# disable Wayland on Hi1710 chipsets
ATTR{vendor}=="0x19e5", ATTR{device}=="0x1711", RUN+="/usr/libexec/gdm-disable-wayland"
# disable Wayland when using the proprietary nvidia driver
DRIVER=="nvidia", RUN+="/usr/libexec/gdm-disable-wayland"
# disable Wayland if modesetting is disabled
IMPORT{cmdline}="nomodeset", RUN+="/usr/libexec/gdm-disable-wayland"

从上面可看出,只要用了the proprietary nvidia driver 或 内核禁用了modeset,这个规则都会禁用Wayland。 好吧,有点跑题了,打住。

参考文档

https://stackoverflow.com/questions/12799653/qdebug-not-displaying-anything/50650124#50650124

https://bugzilla.redhat.com/show_bug.cgi?id=1227295#c10

https://bugzilla.redhat.com/show_bug.cgi?id=1227295#c15

https://bugzilla.redhat.com/show_bug.cgi?id=1227295#c29