QStyleHelper Save

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

Project README

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)

https://user-images.githubusercontent.com/12413639/216264921-933c2670-b1d5-4b67-8624-c5c8aaf39ecd.mp4

https://user-images.githubusercontent.com/12413639/216384897-b8f63c3a-2658-4637-acaa-d85e74e75869.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();
}
}
Open Source Agenda is not affiliated with "QStyleHelper" Project. README Source: mguludag/QStyleHelper
Stars
28
Open Issues
2
Last Commit
1 year ago
License
MIT

Open Source Agenda Badge

Open Source Agenda Rating