Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Switching Styles at runtime?



  • Hi there..

    I managed to switch the Style of my Application in the main.cpp, with

        if(CurrentTheme == "dark"){
            // Load the dark application style
            QFile styleFile(":/style/style/TGS_Dark.qss");
            styleFile.open( QFile::ReadOnly );
            // Apply the loaded stylesheet
            QString style( styleFile.readAll() );
            a.setStyleSheet(style);
        }else{
            // Set default fusion style
            QApplication::setStyle(QStyleFactory::create("Fusion"));
        }
    

    Now, i want to change the Theme of the Window, when the MainWindow is opened, by having the User being able to Select a Theme from a ComboBox (currently holding the Items "dark" and "light")

    I can change the Style to the dark theme with:

        if(CurrentTheme == "dark"){
            // Load the dark application style
            QFile styleFile(":/style/style/TGS_Dark.qss");
            styleFile.open( QFile::ReadOnly );
            // Apply the loaded stylesheet
            QString styledark( styleFile.readAll() );
            qApp->setStyleSheet(styledark);
        }else{
            // Set default fusion style ?
        }
    

    But i can´t simply copy the Code for the fusion theme over here..
    How can i switch back to fusion, like for the dark Style?



  • @SGaist Not with my past method..

    My current method involves a Change of the Palette of fusion, to get dark mode.. and simply switch palettes from lightPalette to darkPalette.

    New problem following...


  • Lifetime Qt Champion

    Hi,

    You can do the same or even use the string version.

    See this example.



  • @SGaist Not with my past method..

    My current method involves a Change of the Palette of fusion, to get dark mode.. and simply switch palettes from lightPalette to darkPalette.

    New problem following...


Log in to reply