Search code examples
c++qtqt5qstyle

How to "Re-Polish" QApplication with Same Style?


Upon my qt5 application's initialization, the QApplication instance is provided with a custom style class that inherits from QProxyStyle.

This class overwrites the void polish(QPalette &palette) function in order to change the general color scheme for the application.

For example:

#include "mycolormanager.h"

void MyStyle::polish(QPalette &palette) {
    palette.setBrush(QPalette::Window, MyColorManager::getWindowColor());
    palette.setColor(QPalette::WindowText, MyColorManager::getTextColor());
    palette.setColor(QPalette::Disabled, QPalette::WindowText, MyColorManager::getDisabledTextColor());
    palette.setColor(QPalette::Base, MyColorManager::getBaseColor());
    // etc...
}

However, I wish to allow users to customize the color scheme of the application. Once the user has selected new colors, I need someway to "reapply" the style, so the function can run again and obtain the new colors from the hypothetical "color manager" class.

How would this be done?


Solution

  • Instead you can call below static function and apply the desired palette.

    void QApplication::setPalette(const QPalette &palette, const char *className = nullptr);
    

    Create the new palette object after selecting the new color scheme and just pass it to above function. You can specify classname, if you want the change to apply for a specific widget.