Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Reload SystemPalette after setStyleSheet()
QtWS25 Last Chance

Reload SystemPalette after setStyleSheet()

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ProgSysP Offline
    ProgSysP Offline
    ProgSys
    wrote on last edited by
    #1

    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
    }
    
    1 Reply Last reply
    0
    • jpnurmiJ Offline
      jpnurmiJ Offline
      jpnurmi
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      1
      • ProgSysP Offline
        ProgSysP Offline
        ProgSys
        wrote on last edited by
        #3

        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"));
        	});
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved