Giter Site home page Giter Site logo

mguludag / qstylehelper Goto Github PK

View Code? Open in Web Editor NEW
28.0 2.0 2.0 107 KB

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

License: MIT License

C++ 100.00%
linux qml qstyle qt qt5-gui qtwidgets windows winui acrylic mica

qstylehelper's Introduction

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();
}
}

qstylehelper's People

Contributors

mguludag avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

crystiol ihmin

qstylehelper's Issues

Possibility of a QML Example project?

Do you have a QML example project that replicates that in the video please?

I've gotten the Title bar to work correctly with QStyleHelper::setTitleBarDarkColor(); on an existing project
but not able to get the mica background in the window working correctly on that or a new test project, I assume there's some flags that need to be set on either side.

The project is great by the way thank you, my App looks so much better in dark mode without that horrible white Title Bar!

I wonder if there is a way to replicate the Mica effect in a QML component ๐Ÿค” it would look amazing since Qt aren't interested in their Apps looking good on Windows.

Cheers

Additional Features / Roadmap

  1. Which features will not work if i use stylesheets?
  2. Are you planning MacOS and Linux auto switching support?
    • for example when the dark / light mode changes with day / night
  3. Do you have any roadmap?
  4. You only mention qt5, is qt6 supported as well?

Thanks a lot :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.