Reload SystemPalette after setStyleSheet()
Solved
QML and Qt Quick
-
Hi,
I my app you can set different styles, like a dark style, to more customize the look to your liking. To keep the look consistent for my QQuickWidgets I use the SystemPalette element, but the problem is that it doesn't update after I call setStyleSheet() (or setPalette()) in my code.
How can I update the SystemPalette with a new pallette, so the style of my QQuickWidgets fits the overall theme?
Do I actually need to create my own data structure and manually update it, or is there a easier way?Thanks for your help!
Some code:
TestWindowApp::TestWindowApp(QWidget *parent):QMainWindow(parent) { setupUi(this); connect(actionThemeDark, &QAction::triggered, this, [this]{; QFile f(QString(RESOURCES_PATH)+"/styles/dark/style.qss"); if (f.open(QFile::ReadOnly | QFile::Text)){ setStyleSheet(QString(f.readAll()).arg(QString(RESOURCES_PATH)+"/styles/dark" )); setPalette(palette());//try1 timelineQuickWidget->setPalette(palette());//try2 emit onStyleChanged();//my custom emit }else qDebug() << "Unable to set stylesheet, file not found!"; }); timelineQuickWidget->setSource(QUrl::fromLocalFile(RESOURCES_PATH "/qml/Timeline.qml")); }
QML:
import QtQuick 2.4 Rectangle { SystemPalette { id: activePalette } color: activePalette.window }
-
Not sure if this is the best approach, but it seems to work.
I reload the QML file after the application palette has bean set.connect(this, &TestWindowApp::onStyleChanged, this, [this]{ QApplication::setPalette(palette()); //reload qml timelineQuickWidget->setSource(QUrl()); timelineQuickWidget->engine()->clearComponentCache(); timelineQuickWidget->setSource(QUrl::fromLocalFile(RESOURCES_PATH "/qml/Timeline.qml")); });