Skip to content

A Helper class for managing QStyle, QPalette, TitleBar Color on Windows and auto detect color scheme changes.

License

Notifications You must be signed in to change notification settings

mguludag/QStyleHelper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 

Repository files navigation

QStyleHelper

A Helper class for managing QStyle, QPalette, TitleBar Color on Windows and auto detect color scheme changes.

Features

  • Auto dark mode app style, titlebar and titlebar context menu on windows
  • Supports Mica style on Windows 11 (you have to set background transparent)
  • Supports Acrylic Transparent Blur on Windows 10/11 (you have to set background transparent)
  • Setting your custom dark/light palette
  • Provide signal for system dark/light scheme changes (useful for changing your app icons)
demo.mp4
qml_mica_acrylic.mp4

Usage for Qt Widgets

main.cpp

#include "mainwindow.h"
#include "qstylehelper.hpp"
#include <QApplication>


int main(int argc, char *argv[])
{
    // this function uses new Qt >= 5.15 win32 dark titlebar environment 
    QStyleHelper::setTitleBarDarkColor();

    QApplication a(argc, argv);
    MainWindow w;
    
    // if you want to use Mica or Acrylic Blur you have to set background transparent
    w.setAttribute(Qt::WA_TranslucentBackground);
    
    // this is for Windows 11
    // example use of Mica, second bool parameter is setting acrylic transparent for mica
    QStyleHelper::setMica({w});
    
    // this is for Windows 10/11 but resize performance is slow (deprecated)
    // example use of Acrylic Blur Window, second bool parameter is setting acrylic transparent for it
    QStyleHelper::setAcrylicBlurWindow({w}, true);
    
    
    // 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 
    QStyleHelper::setTitleBarDarkColor({w});
    
    // initialize the instance and set desired look you want, also you can set any custom QPalette for dark and light scheme separately 
    QStyleHelper::instance().setDarkPalette().setWidgetStyle("fusion").setAutoChangePalette(true);
    

    // this connection and QStyleHelper::colorSchemeChanged signal for monitor windows dark/light mode changes
    QObject::connect(&QStyleHelper::instance(), &QStyleHelper::colorSchemeChanged, [&w](bool dark) 
                     { QStyleHelper::setMica({w}); QStyleHelper::setTitleBarDarkColor({w}, dark); });

    w.show();

    return a.exec();
}

Usage for Qt Quick

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "qstylehelper.hpp"

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    // if your Qt version older than 5.15 use it like this for win32 dark titlebar environment
    QStyleHelper::setTitleBarDarkColor(QGuiApplication::allWindows());
    
    // this is for Windows 11
    // example use of Mica, second bool parameter is setting acrylic transparent for mica
    QStyleHelper::setMica(QGuiApplication::allWindows(), true);
    
    // this is for Windows 10/11 but resize performance is slow (deprecated)
    // example use of Acrylic Blur Window, second bool parameter is setting acrylic transparent for it
    QStyleHelper::setAcrylicBlurWindow(QGuiApplication::allWindows());

    // this connection and QStyleHelper::colorSchemeChanged signal for monitor windows dark/light mode changes
    QObject::connect(&QStyleHelper::instance(), &QStyleHelper::colorSchemeChanged, [](bool b)
    { QStyleHelper::setMica(QGuiApplication::allWindows(), true); QStyleHelper::setTitleBarDarkColor(QGuiApplication::allWindows(), b); });

    return app.exec();
}
}

About

A Helper class for managing QStyle, QPalette, TitleBar Color on Windows and auto detect color scheme changes.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages