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. Qt detect system dark/light mode?
Forum Update on Monday, May 27th 2025

Qt detect system dark/light mode?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.1k 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.
  • G Offline
    G Offline
    Gertio
    wrote on 29 Jul 2023, 10:44 last edited by
    #1

    How dows Qt detect system dark/light mode?
    I've tried

    QGuiApplication::styleHints()->colorScheme();
    

    but it returns

    Qt::ColorScheme::Unknown
    

    if called before the QGuiApplication constructor, so before I actually have an application.
    Is there a chance I can get the ColorScheme of the OS itself before the app creation?

    C D J 3 Replies Last reply 29 Jul 2023, 11:10
    1
    • G Gertio
      29 Jul 2023, 10:44

      How dows Qt detect system dark/light mode?
      I've tried

      QGuiApplication::styleHints()->colorScheme();
      

      but it returns

      Qt::ColorScheme::Unknown
      

      if called before the QGuiApplication constructor, so before I actually have an application.
      Is there a chance I can get the ColorScheme of the OS itself before the app creation?

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 29 Jul 2023, 11:10 last edited by
      #2

      @Gertio said in Qt detect system dark/light mode?:

      Is there a chance I can get the ColorScheme of the OS itself before the app creation?

      As you can see - no.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • G Gertio
        29 Jul 2023, 10:44

        How dows Qt detect system dark/light mode?
        I've tried

        QGuiApplication::styleHints()->colorScheme();
        

        but it returns

        Qt::ColorScheme::Unknown
        

        if called before the QGuiApplication constructor, so before I actually have an application.
        Is there a chance I can get the ColorScheme of the OS itself before the app creation?

        D Offline
        D Offline
        DerReisende
        wrote on 29 Jul 2023, 15:18 last edited by DerReisende
        #3

        @Gertio If you want to figure out whether your windows system is dark mode or not you can use the following code:

        static bool isDarkMode() {
                bool result{false};
                HKEY hKey;
        
                const auto lRes = RegOpenKeyExW(HKEY_CURRENT_USER,
                                                L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
                                                0, KEY_READ, &hKey);
                if (lRes == ERROR_SUCCESS) {
                    DWORD usesLightTheme;
                    const auto res = GetDWORDRegKey(hKey, L"AppsUseLightTheme", usesLightTheme);
                    if (res == ERROR_SUCCESS) {
                        //qDebug() << "Apps use light theme: " << usesLightTheme;
                        result = !usesLightTheme;
                    }
                } else
                    qCritical() << "Failed to query dark mode";
        
                RegCloseKey(hKey);
                return result;
            }
        
        LONG GetDWORDRegKey(HKEY hKey, const std::wstring &strValueName, DWORD &nValue) {
            nValue = -1;
            DWORD dwBufferSize(sizeof(DWORD));
            DWORD nResult(0);
            LONG nError = ::RegQueryValueEx(hKey,
                                            strValueName.c_str(),
                                            nullptr,
                                            nullptr,
                                            reinterpret_cast<LPBYTE>(&nResult),
                                            &dwBufferSize);
            if (ERROR_SUCCESS == nError) {
                nValue = nResult;
            }
            return nError;
        }
        
        1 Reply Last reply
        1
        • G Gertio
          29 Jul 2023, 10:44

          How dows Qt detect system dark/light mode?
          I've tried

          QGuiApplication::styleHints()->colorScheme();
          

          but it returns

          Qt::ColorScheme::Unknown
          

          if called before the QGuiApplication constructor, so before I actually have an application.
          Is there a chance I can get the ColorScheme of the OS itself before the app creation?

          J Offline
          J Offline
          JonB
          wrote on 29 Jul 2023, 16:26 last edited by
          #4

          @Gertio OOI, why do you want this setting prior to creating a QGuiApplication?

          G 1 Reply Last reply 29 Jul 2023, 21:17
          0
          • J JonB
            29 Jul 2023, 16:26

            @Gertio OOI, why do you want this setting prior to creating a QGuiApplication?

            G Offline
            G Offline
            Gertio
            wrote on 29 Jul 2023, 21:17 last edited by
            #5

            @JonB well there are reasons, I want to set the default style based on wether the user has dark mode on or not, but I need to set it with

            QGuiApplication::setStyle(QStyle *style)
            

            before it is created to not break/disable CLI arguments (e.g. -style fusion) and environment variables.

            1 Reply Last reply
            0

            1/5

            29 Jul 2023, 10:44

            • Login

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