Qt detect system dark/light mode?
-
How dows Qt detect system dark/light mode?
I've triedQGuiApplication::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? -
@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.
-
@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; }
-
@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.