Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Theme support for app
QtWS25 Last Chance

Theme support for app

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 3 Posters 5.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.
  • T Offline
    T Offline
    t0msk
    wrote on last edited by
    #1

    Hello, I have a whole stylesheet (css) inside styleSheet property of MainWindow.

    I would like to ask you how to make possible that user could change a theme of app.

    My idea is to create 2 files. 1st file will be for "light" theme and 2nd file will be for "dark" theme, these files contains a stylesheet.

    And if user selects a light theme then app will load a stylesheet from 1st file and if user selects a dark theme then app will load that second one.

    Please how to do that? And is this good aproach? Or is something better?

    Thank you

    Student who loves C/C++

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Have your two stylesheets in your application resources and load the appropriate one when the user makes its selection.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • T Offline
        T Offline
        t0msk
        wrote on last edited by t0msk
        #3

        Yes, I find

        qApp->setStyleSheet();
        

        but how to access a object App inside MainWindow?

        Student who loves C/C++

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Using the qApp macro that is available when you #include <QApplication> ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • T Offline
            T Offline
            t0msk
            wrote on last edited by
            #5

            Ah yes, thanks, but this is my code:

            #include "settings.h"
            #include "ui_settings.h"
            
            #include <QApplication>
            
            settings::settings(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::settings)
            {
                ui->setupUi(this);
            }
            
            settings::~settings()
            {
                delete ui;
            }
            
            void settings::on_pushButton_clicked()
            {
                qApp->setStyleSheet("QMainWindow { background: rgb(130, 255, 47); }");
                this->close();
            }
            

            It is a settings dialog, why it doesn't change a background of MainWindow?

            Student who loves C/C++

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @t0msk said in Theme support for app:

              "QMainWindow { background: rgb(130, 255, 47); }");

              Hi
              You tell that only objects of type QMainWindow would be affected
              and that do not include a QDialog.

              Please see selector types
              http://doc.qt.io/qt-5/stylesheet-syntax.html

              Its pretty important to understand or it becomes a mess to handle :)

              T 1 Reply Last reply
              0
              • mrjjM mrjj

                @t0msk said in Theme support for app:

                "QMainWindow { background: rgb(130, 255, 47); }");

                Hi
                You tell that only objects of type QMainWindow would be affected
                and that do not include a QDialog.

                Please see selector types
                http://doc.qt.io/qt-5/stylesheet-syntax.html

                Its pretty important to understand or it becomes a mess to handle :)

                T Offline
                T Offline
                t0msk
                wrote on last edited by t0msk
                #7

                @mrjj said in Theme support for app:

                @t0msk said in Theme support for app:

                "QMainWindow { background: rgb(130, 255, 47); }");

                Hi
                You tell that only objects of type QMainWindow would be affected
                and that do not include a QDialog.

                Please see selector types
                http://doc.qt.io/qt-5/stylesheet-syntax.html

                Its pretty important to understand or it becomes a mess to handle :)

                Yes I understand, but it doesn't change a different window (QMainWindow). I want to apply that css for whole app.

                Student who loves C/C++

                mrjjM 1 Reply Last reply
                0
                • T t0msk

                  @mrjj said in Theme support for app:

                  @t0msk said in Theme support for app:

                  "QMainWindow { background: rgb(130, 255, 47); }");

                  Hi
                  You tell that only objects of type QMainWindow would be affected
                  and that do not include a QDialog.

                  Please see selector types
                  http://doc.qt.io/qt-5/stylesheet-syntax.html

                  Its pretty important to understand or it becomes a mess to handle :)

                  Yes I understand, but it doesn't change a different window (QMainWindow). I want to apply that css for whole app.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @t0msk
                  ok in that case, you should use
                  setStyleSheet of the QApp
                  http://doc.qt.io/qt-5/qapplication.html#styleSheet-prop

                  But again, if u say "only mainwindow" in the ccs file, it wont matter if you set it on application. it still only applies to Mainwindow types of object.
                  ok ?
                  Im asking again as the sample you show is wrong on a logical level

                  QDialog(parent), <<< its a DIALOG
                  void settings::on_pushButton_clicked()
                  {
                  qApp->setStyleSheet("QMainWindow { background: rgb(130, xxx
                  You ONLY affecting mainwindows.
                  So this can never work if you think that QDialog will use settings assigned to QMainWindow type.

                  T 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @t0msk
                    ok in that case, you should use
                    setStyleSheet of the QApp
                    http://doc.qt.io/qt-5/qapplication.html#styleSheet-prop

                    But again, if u say "only mainwindow" in the ccs file, it wont matter if you set it on application. it still only applies to Mainwindow types of object.
                    ok ?
                    Im asking again as the sample you show is wrong on a logical level

                    QDialog(parent), <<< its a DIALOG
                    void settings::on_pushButton_clicked()
                    {
                    qApp->setStyleSheet("QMainWindow { background: rgb(130, xxx
                    You ONLY affecting mainwindows.
                    So this can never work if you think that QDialog will use settings assigned to QMainWindow type.

                    T Offline
                    T Offline
                    t0msk
                    wrote on last edited by t0msk
                    #9

                    @mrjj said in Theme support for app:

                    @t0msk
                    ok in that case, you should use
                    setStyleSheet of the QApp
                    http://doc.qt.io/qt-5/qapplication.html#styleSheet-prop

                    But again, if u say "only mainwindow" in the ccs file, it wont matter if you set it on application. it still only applies to Mainwindow types of object.
                    ok ?
                    Im asking again as the sample you show is wrong on a logical level

                    QDialog(parent), <<< its a DIALOG
                    void settings::on_pushButton_clicked()
                    {
                    qApp->setStyleSheet("QMainWindow { background: rgb(130, xxx
                    You ONLY affecting mainwindows.
                    So this can never work if you think that QDialog will use settings assigned to QMainWindow type.

                    You don't understand me. I know that QMainWindow is not QDialog..

                    But I have 2 windows, one is QDialog (settings window) and second is QMainWindow (main program), I know that design of QDialog doesn't change, but if I push button it doesn't change design of the second window QMainWindow (main program).

                    Ah it works now, it didn't work because I had fixed css in ui file. So it means that if I set some css through Qt Designer then

                    qApp->setStyleSheet
                    

                    doesn't override it?

                    Student who loves C/C++

                    mrjjM 1 Reply Last reply
                    0
                    • T t0msk

                      @mrjj said in Theme support for app:

                      @t0msk
                      ok in that case, you should use
                      setStyleSheet of the QApp
                      http://doc.qt.io/qt-5/qapplication.html#styleSheet-prop

                      But again, if u say "only mainwindow" in the ccs file, it wont matter if you set it on application. it still only applies to Mainwindow types of object.
                      ok ?
                      Im asking again as the sample you show is wrong on a logical level

                      QDialog(parent), <<< its a DIALOG
                      void settings::on_pushButton_clicked()
                      {
                      qApp->setStyleSheet("QMainWindow { background: rgb(130, xxx
                      You ONLY affecting mainwindows.
                      So this can never work if you think that QDialog will use settings assigned to QMainWindow type.

                      You don't understand me. I know that QMainWindow is not QDialog..

                      But I have 2 windows, one is QDialog (settings window) and second is QMainWindow (main program), I know that design of QDialog doesn't change, but if I push button it doesn't change design of the second window QMainWindow (main program).

                      Ah it works now, it didn't work because I had fixed css in ui file. So it means that if I set some css through Qt Designer then

                      qApp->setStyleSheet
                      

                      doesn't override it?

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @t0msk
                      Ah so the fact that escaped you is
                      that stylesheet are cascading from parent to childs.

                      Since the mainwindow is NOT a child of the dialog, setting it there will not change
                      any QMainwindows.

                      Anyways, use QApp setStyleSheet will.

                      1 Reply Last reply
                      0
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #11

                        @t0msk said in Theme support for app:

                        doesn't override it?

                        Depends on order.

                        You set QApp in main

                        In setupUI, any style sheet will override.

                        Do you need multiple stylesheets? I always makes a mess when i try.
                        One big one set to App in main always works the cleanest.

                        T 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @t0msk said in Theme support for app:

                          doesn't override it?

                          Depends on order.

                          You set QApp in main

                          In setupUI, any style sheet will override.

                          Do you need multiple stylesheets? I always makes a mess when i try.
                          One big one set to App in main always works the cleanest.

                          T Offline
                          T Offline
                          t0msk
                          wrote on last edited by
                          #12

                          @mrjj
                          I have main, mainwindow and settings. My idea is that, in main it will load stylesheet from config QSettings.. and then if user want to change it, he will change it in settings dialog, settings dialog then write that change to QSettings and apply a new stylesheet, it works, but I don't know, why it (settings) doesn't overwrite stylesheet defined in QtDesigner.

                          Student who loves C/C++

                          mrjjM 1 Reply Last reply
                          0
                          • T t0msk

                            @mrjj
                            I have main, mainwindow and settings. My idea is that, in main it will load stylesheet from config QSettings.. and then if user want to change it, he will change it in settings dialog, settings dialog then write that change to QSettings and apply a new stylesheet, it works, but I don't know, why it (settings) doesn't overwrite stylesheet defined in QtDesigner.

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #13

                            @t0msk said in Theme support for app:

                            doesn't overwrite stylesheet defined in QtDesigner.

                            It should. If you set it on QApp()->setStyleSheet in the dialog
                            But not tested it. Might not override when already set.
                            Note that Designer do NOT do anything special.
                            It simply calls setStyleSheet in setupUI

                            Try
                            setStyleSheet(QString());
                            in ctor of main and see if it then overrides.
                            Note. do not use "" as its not the same as no stylesheet

                            T 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              @t0msk said in Theme support for app:

                              doesn't overwrite stylesheet defined in QtDesigner.

                              It should. If you set it on QApp()->setStyleSheet in the dialog
                              But not tested it. Might not override when already set.
                              Note that Designer do NOT do anything special.
                              It simply calls setStyleSheet in setupUI

                              Try
                              setStyleSheet(QString());
                              in ctor of main and see if it then overrides.
                              Note. do not use "" as its not the same as no stylesheet

                              T Offline
                              T Offline
                              t0msk
                              wrote on last edited by
                              #14

                              @mrjj said in Theme support for app:

                              QString(

                              I tried:

                              MainWindow::MainWindow(QWidget *parent) :
                                  QMainWindow(parent),
                                  ui(new Ui::MainWindow)
                              {
                                  ui->setupUi(this);
                              
                                  qApp->setStyleSheet(QString("QMainWindow { background: rgb(130, 255, 47); }"));
                              }
                              

                              and it didn't override stylesheet defined in Designer. I tried it in main.cpp too and no success.

                              Student who loves C/C++

                              mrjjM 1 Reply Last reply
                              0
                              • T t0msk

                                @mrjj said in Theme support for app:

                                QString(

                                I tried:

                                MainWindow::MainWindow(QWidget *parent) :
                                    QMainWindow(parent),
                                    ui(new Ui::MainWindow)
                                {
                                    ui->setupUi(this);
                                
                                    qApp->setStyleSheet(QString("QMainWindow { background: rgb(130, 255, 47); }"));
                                }
                                

                                and it didn't override stylesheet defined in Designer. I tried it in main.cpp too and no success.

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @t0msk said in Theme support for app:
                                Lets assume that the cascading effect excludes widget with non empty stylesheets.
                                Can you try.

                                {
                                    ui->setupUi(this);
                                    setStyleSheet( QString() );
                                    qApp->setStyleSheet(QString("QMainWindow { background: rgb(130, 255, 47); }"));
                                }``
                                T 1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @t0msk said in Theme support for app:
                                  Lets assume that the cascading effect excludes widget with non empty stylesheets.
                                  Can you try.

                                  {
                                      ui->setupUi(this);
                                      setStyleSheet( QString() );
                                      qApp->setStyleSheet(QString("QMainWindow { background: rgb(130, 255, 47); }"));
                                  }``
                                  T Offline
                                  T Offline
                                  t0msk
                                  wrote on last edited by
                                  #16

                                  @mrjj
                                  Thank you, this works, but why, I don't understand.. and what is this line?

                                  setStyleSheet( QString() );
                                  

                                  why there is no "qApp->"? It is setStyleSheet for what? Why am I passing empty QString?

                                  Student who loves C/C++

                                  mrjjM 1 Reply Last reply
                                  0
                                  • T t0msk

                                    @mrjj
                                    Thank you, this works, but why, I don't understand.. and what is this line?

                                    setStyleSheet( QString() );
                                    

                                    why there is no "qApp->"? It is setStyleSheet for what? Why am I passing empty QString?

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by mrjj
                                    #17

                                    @t0msk
                                    hi
                                    I clear the stylesheet of mainwindow
                                    this->setStyleSheet( QString() );
                                    the QString() constructs an empty string object.
                                    Which is different from setStyleSheet( "" );
                                    So it seems that it will not override other already set stylesheets.
                                    But if you clear it (by setting to empty.string(). Using "" would be empty stylesheet.)
                                    then it works.

                                    T 1 Reply Last reply
                                    1
                                    • mrjjM mrjj

                                      @t0msk
                                      hi
                                      I clear the stylesheet of mainwindow
                                      this->setStyleSheet( QString() );
                                      the QString() constructs an empty string object.
                                      Which is different from setStyleSheet( "" );
                                      So it seems that it will not override other already set stylesheets.
                                      But if you clear it (by setting to empty.string(). Using "" would be empty stylesheet.)
                                      then it works.

                                      T Offline
                                      T Offline
                                      t0msk
                                      wrote on last edited by
                                      #18

                                      @mrjj
                                      aha, thanks :)

                                      Student who loves C/C++

                                      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