You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt应用忽略Wayland时无法识别用户深色模式设置问题求助

Qt 6.2深色模式检测异常及Wayland核心转储问题

正在开发一款个人应用以学习Qt和C++23,isDarkMode()代码取自Stack Exchange系列站点。代码本身可正常运行,但在我的应用中样式表生成异常,运行时收到Qt警告:Warning: Ignoring WAYLAND_DISPLAY on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland。目前存在两个问题:

  • 实例化QApplication后无法识别用户的深色模式设置(实例化前可正常检测),这是核心问题,我认为是Qt的bug
  • 设置QT_QPA_PLATFORM=wayland时程序会核心转储

环境

  • Ubuntu 22.04
  • C++23
  • GCC-12
  • Qt 6.2

代码

#include <iomanip>
#include <iostream>
#include <QApplication>
#include <QGuiApplication>
#include <QPalette>
#include <QStyleHints>

inline bool isDarkMode() {
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
    const auto scheme = QGuiApplication::styleHints()->colorScheme();
    return scheme == Qt::ColorScheme::Dark;
#else
    const QPalette defaultPalette;
    const auto text = defaultPalette.color(QPalette::WindowText);
    const auto window = defaultPalette.color(QPalette::Window);
    return text.lightness() > window.lightness();
#endif // QT_VERSION
}

int main(int argc, char *argv[])
{
    std::cout << "Version" << std::hex << QT_VERSION << "\n";
    if (isDarkMode())
    {
        std::cout << "using Dark Mode\n";
    }
    else
    {
        std::cout << "Not using Dark Mode\n";
    }

    QApplication theApp(argc, argv);

    if (isDarkMode())
    {
        std::cout << "using Dark Mode\n";
    }
    else
    {
        std::cout << "Not using Dark Mode\n";
    }
}

运行输出

未安装qt6-wayland时的输出

paulc> build/CRQ*
Version60204
using Dark Mode
Warning: Ignoring WAYLAND_DISPLAY on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
Not using Dark Mode
paulc> export QT_QPA_PLATFORM=wayland
paulc> build/CRQ*
Version60204
using Dark Mode
qt.qpa.plugin: Could not find the Qt platform plugin "wayland" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vkkhrdisplay, vnc, xcb.

Aborted (core dumped)
paulc> 

安装qt6-wayland后的输出

paulc> !bu
build/CRQ*
Version60204
using Dark Mode
Not using Dark Mode
paulc> 

内容的提问来源于stack exchange,提问作者pacmaninbw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 04:12:04