├── LICENSE ├── README.md └── include └── qstylehelper.hpp /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Muhammed Galib Uludag 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # QStyleHelper 2 | A Helper class for managing QStyle, QPalette, TitleBar Color on Windows and auto detect color scheme changes. 3 | 4 | ## Features 5 | * Auto dark mode app style, titlebar and titlebar context menu on windows 6 | * Supports Mica style on Windows 11 (you have to set background transparent) 7 | * Supports Acrylic Transparent Blur on Windows 10/11 (you have to set background transparent) 8 | * Setting your custom dark/light palette 9 | * Provide signal for system dark/light scheme changes (useful for changing your app icons) 10 | 11 | 12 | 13 | https://user-images.githubusercontent.com/12413639/216264921-933c2670-b1d5-4b67-8624-c5c8aaf39ecd.mp4 14 | 15 | https://user-images.githubusercontent.com/12413639/216384897-b8f63c3a-2658-4637-acaa-d85e74e75869.mp4 16 | 17 | 18 | 19 | 20 | ## Usage for Qt Widgets 21 | ### main.cpp 22 | ```C++ 23 | #include "mainwindow.h" 24 | #include "qstylehelper.hpp" 25 | #include 26 | 27 | 28 | int main(int argc, char *argv[]) 29 | { 30 | // this function uses new Qt >= 5.15 win32 dark titlebar environment 31 | QStyleHelper::setTitleBarDarkColor(); 32 | 33 | QApplication a(argc, argv); 34 | MainWindow w; 35 | 36 | // if you want to use Mica or Acrylic Blur you have to set background transparent 37 | w.setAttribute(Qt::WA_TranslucentBackground); 38 | 39 | // this is for Windows 11 40 | // example use of Mica, second bool parameter is setting acrylic transparent for mica 41 | QStyleHelper::setMica({w}); 42 | 43 | // this is for Windows 10/11 but resize performance is slow (deprecated) 44 | // example use of Acrylic Blur Window, second bool parameter is setting acrylic transparent for it 45 | QStyleHelper::setAcrylicBlurWindow({w}, true); 46 | 47 | 48 | // if your Qt version older than 5.15 use it like this for win32 dark titlebar environment and also you have to call once for any subwindows 49 | QStyleHelper::setTitleBarDarkColor({w}); 50 | 51 | // initialize the instance and set desired look you want, also you can set any custom QPalette for dark and light scheme separately 52 | QStyleHelper::instance().setDarkPalette().setWidgetStyle("fusion").setAutoChangePalette(true); 53 | 54 | 55 | // this connection and QStyleHelper::colorSchemeChanged signal for monitor windows dark/light mode changes 56 | QObject::connect(&QStyleHelper::instance(), &QStyleHelper::colorSchemeChanged, [&w](bool dark) 57 | { QStyleHelper::setMica({w}); QStyleHelper::setTitleBarDarkColor({w}, dark); }); 58 | 59 | w.show(); 60 | 61 | return a.exec(); 62 | } 63 | ``` 64 | 65 | ## Usage for Qt Quick 66 | ### main.cpp 67 | ```C++ 68 | #include 69 | #include 70 | #include "qstylehelper.hpp" 71 | 72 | int main(int argc, char *argv[]) 73 | { 74 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 75 | QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); 76 | #endif 77 | QGuiApplication app(argc, argv); 78 | 79 | QQmlApplicationEngine engine; 80 | const QUrl url(QStringLiteral("qrc:/main.qml")); 81 | QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, 82 | &app, [url](QObject *obj, const QUrl &objUrl) { 83 | if (!obj && url == objUrl) 84 | QCoreApplication::exit(-1); 85 | }, Qt::QueuedConnection); 86 | engine.load(url); 87 | 88 | // if your Qt version older than 5.15 use it like this for win32 dark titlebar environment 89 | QStyleHelper::setTitleBarDarkColor(QGuiApplication::allWindows()); 90 | 91 | // this is for Windows 11 92 | // example use of Mica, second bool parameter is setting acrylic transparent for mica 93 | QStyleHelper::setMica(QGuiApplication::allWindows(), true); 94 | 95 | // this is for Windows 10/11 but resize performance is slow (deprecated) 96 | // example use of Acrylic Blur Window, second bool parameter is setting acrylic transparent for it 97 | QStyleHelper::setAcrylicBlurWindow(QGuiApplication::allWindows()); 98 | 99 | // this connection and QStyleHelper::colorSchemeChanged signal for monitor windows dark/light mode changes 100 | QObject::connect(&QStyleHelper::instance(), &QStyleHelper::colorSchemeChanged, [](bool b) 101 | { QStyleHelper::setMica(QGuiApplication::allWindows(), true); QStyleHelper::setTitleBarDarkColor(QGuiApplication::allWindows(), b); }); 102 | 103 | return app.exec(); 104 | } 105 | } 106 | ``` 107 | -------------------------------------------------------------------------------- /include/qstylehelper.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2023 Muhammed Galib Uludag 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #ifndef QSTYLEHELPER_HPP 26 | #define QSTYLEHELPER_HPP 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #ifdef QT_WIDGETS_LIB 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #endif 41 | 42 | #if defined(Q_CC_GNU) || defined(Q_CC_CLANG) 43 | #define DEPRECATED(func) func __attribute__ ((deprecated)) 44 | #elif defined(Q_CC_MSVC) 45 | #define DEPRECATED(func) __declspec(deprecated) func 46 | #endif 47 | 48 | #if defined(Q_OS_WIN) && defined(Q_CC_MSVC) 49 | #include 50 | #include 51 | 52 | #pragma comment(lib, "Dwmapi.lib") 53 | 54 | enum DwmWindowAttribute : uint 55 | { 56 | UseImmersiveDarkMode = 20, 57 | WindowCornerPreference = 33, 58 | BorderColor, 59 | CaptionColor, 60 | TextColor, 61 | VisibleFrameBorderThickness, 62 | SystemBackdropType, 63 | UndocumentedSystemBackdropType = 1029, 64 | Last 65 | }; 66 | 67 | // 1903 18362 68 | enum PreferredAppMode 69 | { 70 | Default, 71 | AllowDark, 72 | ForceDark, 73 | ForceLight, 74 | Max 75 | }; 76 | 77 | using fnAllowDarkModeForApp = bool (WINAPI *)(bool allow); // ordinal 135, in 1809 78 | 79 | using fnSetPreferredAppMode = PreferredAppMode (WINAPI *)(PreferredAppMode appMode); // ordinal 135, in 1903 80 | 81 | #endif 82 | 83 | #if defined(Q_OS_WIN) && defined(Q_CC_MSVC) 84 | 85 | typedef enum _WINDOWCOMPOSITIONATTRIB 86 | { 87 | WCA_THEME_ATTRIBUTES = 10, 88 | WCA_ACCENT_POLICY = 19, 89 | WCA_FREEZE_REPRESENTATION = 20, 90 | WCA_MICA = 26 91 | } WINDOWCOMPOSITIONATTRIB; 92 | 93 | typedef struct _WINDOWCOMPOSITIONATTRIBDATA 94 | { 95 | WINDOWCOMPOSITIONATTRIB Attrib; 96 | PVOID pvData; 97 | SIZE_T cbData; 98 | } WINDOWCOMPOSITIONATTRIBDATA; 99 | 100 | typedef enum _ACCENT_STATE 101 | { 102 | ACCENT_DISABLED = 0, 103 | ACCENT_ENABLE_GRADIENT = 1, 104 | ACCENT_ENABLE_TRANSPARENTGRADIENT = 2, 105 | ACCENT_ENABLE_BLURBEHIND = 3, 106 | ACCENT_ENABLE_ACRYLICBLURBEHIND = 4, // RS4 1803 107 | ACCENT_ENABLE_HOSTBACKDROP = 5, // RS5 1809 108 | ACCENT_INVALID_STATE = 6 109 | } ACCENT_STATE; 110 | 111 | typedef struct _ACCENT_POLICY 112 | { 113 | ACCENT_STATE AccentState; 114 | DWORD AccentFlags; 115 | DWORD GradientColor; 116 | DWORD AnimationId; 117 | } ACCENT_POLICY; 118 | 119 | using pfnGetWindowCompositionAttribute = BOOL (WINAPI *)(HWND, WINDOWCOMPOSITIONATTRIBDATA*); 120 | 121 | using pfnSetWindowCompositionAttribute = BOOL (WINAPI *)(HWND, WINDOWCOMPOSITIONATTRIBDATA*); 122 | 123 | #endif 124 | 125 | 126 | class QStyleHelper : public QObject 127 | { 128 | Q_OBJECT 129 | public: 130 | static QStyleHelper &instance(); 131 | ~QStyleHelper(); 132 | 133 | static void setTitleBarDarkColor(); 134 | static void setTitleBarDarkColor(QList &&windows, bool dark = true); 135 | static void setMica(QList &&windows, bool acrylic = false); 136 | 137 | DEPRECATED(static void setAcrylicBlurWindow(QList &&windows, bool acrylic = false)); 138 | 139 | #if defined(QT_WIDGETS_LIB) && defined(Q_CC_MSVC) 140 | static void setTitleBarDarkColor(std::initializer_list> &&windows, bool dark = true); 141 | static void setMica(std::initializer_list> &&windows, bool acrylic = false); 142 | DEPRECATED(static void setAcrylicBlurWindow(std::initializer_list> &&windows, bool acrylic = false)); 143 | #endif 144 | 145 | #if defined(QT_WIDGETS_LIB) 146 | static QStringList QStyleNames(); 147 | QStyleHelper &setWidgetStyle(const QString& styleName); 148 | QStyleHelper &setCustomDarkPalette(const QPalette &newCustomDarkPalette); 149 | QStyleHelper &setCustomLightPalette(const QPalette &newCustomLightPalette); 150 | QStyleHelper &setAutoChangePalette(bool autochange); 151 | QStyleHelper &setDarkPalette(); 152 | QStyleHelper &setLightPalette(); 153 | #endif 154 | 155 | signals: 156 | void colorSchemeChanged(bool isDark); 157 | 158 | protected: 159 | void timerEvent(QTimerEvent *) override; 160 | bool isDark(); 161 | 162 | private: 163 | QStyleHelper(); 164 | static void __setAcyrlicBlur(WId hwnd, bool acrylic); 165 | static void __setMica(WId hwnd, bool acrylic); 166 | static void __setTitlebarDark(WId hwnd, bool dark); 167 | 168 | #ifdef QT_WIDGETS_LIB 169 | struct QPaletteHelper 170 | { 171 | QPaletteHelper(); 172 | 173 | void setCustomDarkPalette(const QPalette &newCustomDarkPalette); 174 | void setCustomLightPalette(const QPalette &newCustomLightPalette); 175 | void setPalette(bool dark); 176 | const bool& style(); 177 | 178 | 179 | private: 180 | bool mIsDarkPalette; 181 | QPalette mCustomDarkPalette; 182 | QPalette mCustomLightPalette; 183 | const QColor lightGray{190, 190, 190}; 184 | const QColor gray{164, 166, 168}; 185 | const QColor midDarkGray{68, 68, 68}; 186 | const QColor darkGray{53, 53, 53}; 187 | const QColor black{25, 25, 25}; 188 | const QColor blue{42, 130, 218}; 189 | }; 190 | #endif 191 | 192 | private: 193 | bool mIsDark; 194 | bool mAutoChangePalette; 195 | QBasicTimer mTimer; 196 | QSettings mSettings; 197 | #ifdef QT_WIDGETS_LIB 198 | QPaletteHelper mPaletteHelper; 199 | #endif 200 | }; 201 | 202 | 203 | inline QStyleHelper::QStyleHelper() : 204 | mIsDark(false), mSettings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", 205 | QSettings::NativeFormat) 206 | { 207 | mTimer.start(500, Qt::VeryCoarseTimer, this); 208 | } 209 | 210 | inline QStyleHelper &QStyleHelper::instance() 211 | { 212 | static QStyleHelper d; 213 | return d; 214 | } 215 | 216 | inline QStyleHelper::~QStyleHelper() 217 | { 218 | mTimer.stop(); 219 | } 220 | 221 | inline void QStyleHelper::setTitleBarDarkColor() 222 | { 223 | #if defined(Q_OS_WIN) 224 | qputenv("QT_QPA_PLATFORM", "windows:darkmode=1"); 225 | #endif // Q_OS_WINDOWS 226 | } 227 | 228 | #if defined(QT_WIDGETS_LIB) && defined(Q_CC_MSVC) 229 | inline void QStyleHelper::setTitleBarDarkColor(std::initializer_list> &&windows, bool dark) 230 | { 231 | #if defined(Q_OS_WIN) 232 | for (auto &w : windows) { 233 | auto hwnd = w.get().winId(); 234 | __setTitlebarDark(hwnd, dark); 235 | } 236 | #endif 237 | } 238 | 239 | inline void QStyleHelper::setAcrylicBlurWindow(std::initializer_list> &&windows, bool acrylic) 240 | { 241 | #if defined(Q_OS_WIN) && defined(Q_CC_MSVC) && defined(QT_WIDGETS_LIB) 242 | for (auto &w : windows) { 243 | auto hwnd = w.get().winId(); 244 | __setAcyrlicBlur(hwnd, acrylic); 245 | } 246 | #endif 247 | } 248 | 249 | inline void QStyleHelper::setMica(std::initializer_list> &&windows, bool acrylic) 250 | { 251 | #if defined(Q_OS_WIN) && defined(Q_CC_MSVC) && defined(QT_WIDGETS_LIB) 252 | for (auto &w : windows) { 253 | auto hwnd = w.get().winId(); 254 | __setMica(hwnd, acrylic); 255 | } 256 | #endif 257 | } 258 | #endif 259 | 260 | inline void QStyleHelper::setTitleBarDarkColor(QList &&windows, bool dark) 261 | { 262 | #if defined(Q_OS_WIN) 263 | for (auto &w : windows) { 264 | auto hwnd = w->winId(); 265 | __setTitlebarDark(hwnd, dark); 266 | } 267 | #endif 268 | } 269 | 270 | inline void QStyleHelper::setAcrylicBlurWindow(QList &&windows, bool acrylic) 271 | { 272 | #if defined(Q_OS_WIN) && defined(Q_CC_MSVC) 273 | for (auto &w : windows) { 274 | auto hwnd = w->winId(); 275 | __setAcyrlicBlur(hwnd, acrylic); 276 | } 277 | #endif 278 | } 279 | 280 | inline void QStyleHelper::setMica(QList &&windows, bool acrylic) 281 | { 282 | #if defined(Q_OS_WIN) && defined(Q_CC_MSVC) && defined(QT_QML_LIB) 283 | for (auto &w : windows) { 284 | auto hwnd = w->winId(); 285 | __setMica(hwnd, acrylic); 286 | } 287 | #endif 288 | } 289 | 290 | inline void QStyleHelper::__setAcyrlicBlur(WId hwnd, bool acrylic) 291 | { 292 | #if defined(Q_OS_WIN) && defined(Q_CC_MSVC) 293 | static HMODULE hUser = GetModuleHandle(L"user32.dll"); 294 | if (hUser){ 295 | static auto setWCA = reinterpret_cast(GetProcAddress(hUser, "SetWindowCompositionAttribute")); 296 | if (setWCA){ 297 | ACCENT_POLICY accent = { acrylic ? ACCENT_ENABLE_ACRYLICBLURBEHIND : ACCENT_ENABLE_BLURBEHIND, 0, 0, 0 }; 298 | WINDOWCOMPOSITIONATTRIBDATA data{WCA_ACCENT_POLICY, &accent, sizeof(accent)}; 299 | setWCA(reinterpret_cast(hwnd), &data); 300 | } 301 | } 302 | #endif 303 | } 304 | 305 | inline void QStyleHelper::__setMica(WId hwnd, bool acrylic) 306 | { 307 | #if defined(Q_OS_WIN) && defined(Q_CC_MSVC) 308 | #if defined(QT_WIDGETS_LIB) && !defined(QT_QML_LIB) 309 | static HMODULE hUser = GetModuleHandle(L"user32.dll"); 310 | if (hUser){ 311 | static auto setWCA = reinterpret_cast(GetProcAddress(hUser, "SetWindowCompositionAttribute")); 312 | if (setWCA){ 313 | ACCENT_POLICY accent = {ACCENT_ENABLE_HOSTBACKDROP, 314 | static_cast((acrylic ? 3 : 2)), 0x0cccccc, 0 }; 315 | WINDOWCOMPOSITIONATTRIBDATA data{WCA_ACCENT_POLICY, &accent, sizeof(accent)}; 316 | setWCA(reinterpret_cast(hwnd), &data); 317 | const MARGINS margins{-1, -1, -1, -1}; 318 | DwmExtendFrameIntoClientArea((HWND)hwnd, &margins); 319 | setWCA(reinterpret_cast(hwnd), &data); 320 | } 321 | } 322 | #endif 323 | const auto build = QOperatingSystemVersion::current().microVersion(); 324 | const BOOL backdrop = acrylic ? (build < 22523 ? 2 : 3) : (build < 22523 ? 1 : 2); 325 | DwmSetWindowAttribute(reinterpret_cast(hwnd), build < 22523 ? 326 | DwmWindowAttribute::UndocumentedSystemBackdropType : DwmWindowAttribute::SystemBackdropType, 327 | &backdrop, sizeof(backdrop)); 328 | #endif 329 | } 330 | 331 | inline void QStyleHelper::__setTitlebarDark(WId hwnd, bool dark) 332 | { 333 | #if defined(Q_OS_WIN) && defined(Q_CC_MSVC) 334 | const BOOL darkBorder = static_cast(dark); 335 | DwmSetWindowAttribute(reinterpret_cast(hwnd), DwmWindowAttribute::UseImmersiveDarkMode, &darkBorder, sizeof(darkBorder)); 336 | static HMODULE hUxTheme = GetModuleHandle(L"uxtheme.dll"); 337 | if (hUxTheme){ 338 | static auto fnPtr_135 = GetProcAddress(hUxTheme, MAKEINTRESOURCEA(135)); 339 | if (fnPtr_135){ 340 | const auto build = QOperatingSystemVersion::current().microVersion(); 341 | if(build < 18362){ 342 | auto fnDarkMode = reinterpret_cast(fnPtr_135); 343 | fnDarkMode(dark); 344 | } 345 | else{ 346 | auto fnDarkMode = reinterpret_cast(fnPtr_135); 347 | fnDarkMode(dark ? PreferredAppMode::ForceDark : PreferredAppMode::ForceLight); 348 | } 349 | } 350 | } 351 | #endif 352 | } 353 | 354 | #if defined(QT_WIDGETS_LIB) 355 | inline QStringList QStyleHelper::QStyleNames() { return QStyleFactory::keys(); } 356 | 357 | inline QStyleHelper &QStyleHelper::setWidgetStyle(const QString &styleName) 358 | { 359 | QApplication::setStyle(QStyleFactory::create(styleName)); 360 | return *this; 361 | } 362 | 363 | inline QStyleHelper &QStyleHelper::setCustomDarkPalette(const QPalette &newCustomDarkPalette) 364 | { 365 | mPaletteHelper.setCustomDarkPalette(newCustomDarkPalette); 366 | return *this; 367 | } 368 | 369 | inline QStyleHelper &QStyleHelper::setCustomLightPalette(const QPalette &newCustomLightPalette) 370 | { 371 | mPaletteHelper.setCustomLightPalette(newCustomLightPalette); 372 | return *this; 373 | } 374 | 375 | inline QStyleHelper &QStyleHelper::setAutoChangePalette(bool autochange) 376 | { 377 | mAutoChangePalette = autochange; 378 | return *this; 379 | } 380 | 381 | inline QStyleHelper &QStyleHelper::setDarkPalette() 382 | { 383 | mPaletteHelper.setPalette(true); 384 | return *this; 385 | } 386 | 387 | inline QStyleHelper &QStyleHelper::setLightPalette() 388 | { 389 | mPaletteHelper.setPalette(false); 390 | return *this; 391 | } 392 | #endif 393 | 394 | inline void QStyleHelper::timerEvent(QTimerEvent *) 395 | { 396 | auto result = isDark(); 397 | if(result == mIsDark){ 398 | mIsDark = !result; 399 | emit colorSchemeChanged(mIsDark); 400 | #if defined(QT_WIDGETS_LIB) 401 | mAutoChangePalette ? mPaletteHelper.setPalette(mIsDark) : void(); 402 | #endif 403 | } 404 | } 405 | 406 | inline bool QStyleHelper::isDark() 407 | { 408 | #ifdef Q_OS_WINDOWS 409 | return mSettings.value("AppsUseLightTheme", true).toBool(); 410 | #elif Q_OS_UNIX 411 | //TODO: add linux equivalent for detecting scheme changes 412 | // org.freedesktop.portal.Desktop /org/freedesktop/portal/desktop org.freedesktop.portal.Settings.Read "org.freedesktop.appearance" "color-scheme" 413 | // https://code.qt.io/cgit/qt/qtbase.git/tree/src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportaltheme.cpp?h=dev#n116 414 | #endif 415 | } 416 | 417 | 418 | 419 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 420 | 421 | 422 | #if defined(QT_WIDGETS_LIB) 423 | inline void QStyleHelper::QPaletteHelper::setPalette(bool dark) 424 | { 425 | QPalette palette{dark ? mCustomDarkPalette : mCustomLightPalette}; 426 | 427 | mIsDarkPalette = dark; 428 | 429 | QApplication::setPalette(palette); 430 | QToolTip::setPalette(palette); 431 | 432 | auto borderBrush = (palette == QApplication::style()->standardPalette() ? palette.midlight() : palette.mid()); 433 | static auto tbStyleSheet = QString("QToolBar { border: 1px solid %1; }"); 434 | static auto app = dynamic_cast(QApplication::instance()); 435 | app->setStyleSheet(tbStyleSheet.arg(borderBrush.color().name())); 436 | } 437 | 438 | inline const bool& QStyleHelper::QPaletteHelper::style() 439 | { 440 | return mIsDarkPalette; 441 | } 442 | 443 | inline void QStyleHelper::QPaletteHelper::setCustomLightPalette(const QPalette &newCustomLightPalette) 444 | { 445 | mCustomLightPalette = newCustomLightPalette; 446 | } 447 | 448 | inline QStyleHelper::QPaletteHelper::QPaletteHelper() 449 | : mCustomDarkPalette(QApplication::style()->standardPalette()), 450 | mCustomLightPalette(QApplication::style()->standardPalette()) 451 | { 452 | mCustomDarkPalette.setColor(QPalette::Window, darkGray); 453 | mCustomDarkPalette.setColor(QPalette::WindowText, Qt::white); 454 | mCustomDarkPalette.setColor(QPalette::Light, lightGray); 455 | mCustomDarkPalette.setColor(QPalette::Midlight, midDarkGray); 456 | mCustomDarkPalette.setColor(QPalette::Dark, darkGray); 457 | mCustomDarkPalette.setColor(QPalette::Mid, midDarkGray); 458 | mCustomDarkPalette.setColor(QPalette::Shadow, black); 459 | mCustomDarkPalette.setColor(QPalette::Base, black); 460 | mCustomDarkPalette.setColor(QPalette::AlternateBase, darkGray); 461 | mCustomDarkPalette.setColor(QPalette::ToolTipBase, darkGray); 462 | mCustomDarkPalette.setColor(QPalette::ToolTipText, Qt::white); 463 | mCustomDarkPalette.setColor(QPalette::Text, Qt::white); 464 | mCustomDarkPalette.setColor(QPalette::Button, darkGray); 465 | mCustomDarkPalette.setColor(QPalette::ButtonText, Qt::white); 466 | mCustomDarkPalette.setColor(QPalette::BrightText, Qt::red); 467 | mCustomDarkPalette.setColor(QPalette::Link, blue); 468 | mCustomDarkPalette.setColor(QPalette::Highlight, blue); 469 | mCustomDarkPalette.setColor(QPalette::HighlightedText, Qt::black); 470 | mCustomDarkPalette.setColor(QPalette::Disabled, QPalette::Text, midDarkGray); 471 | mCustomDarkPalette.setColor(QPalette::Disabled, QPalette::WindowText, midDarkGray); 472 | mCustomDarkPalette.setColor(QPalette::Disabled, QPalette::ButtonText, midDarkGray); 473 | mCustomDarkPalette.setColor(QPalette::Disabled, QPalette::HighlightedText, midDarkGray); 474 | mCustomDarkPalette.setColor(QPalette::Disabled, QPalette::Base, midDarkGray); 475 | mCustomDarkPalette.setColor(QPalette::Disabled, QPalette::Window, midDarkGray); 476 | mCustomDarkPalette.setColor(QPalette::Disabled, QPalette::Highlight, midDarkGray); 477 | } 478 | 479 | inline void QStyleHelper::QPaletteHelper::setCustomDarkPalette(const QPalette &newCustomDarkPalette) 480 | { 481 | mCustomDarkPalette = newCustomDarkPalette; 482 | } 483 | 484 | #endif //QT_WIDGETS_LIB 485 | 486 | #endif // QSTYLEHELPER_HPP 487 | --------------------------------------------------------------------------------