Reload SystemPalette after setStyleSheet()



  • 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
    }
    


  • SystemPalette corresponds to QGuiApplication::palette. Changing the palette of a window does not affect the application palette. Try changing the application palette. Notice also that setPalette(palette()) does not change the palette.



  • 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"));
    	});
    

Log in to reply