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. remove applied stylesheet in Qt

remove applied stylesheet in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 6 Posters 14.7k 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
    Tejas Virpariya
    wrote on 30 Dec 2016, 05:55 last edited by
    #1

    Hi,

    First of all thanks to everybody to maintain this forum and give such a helpful answers.

    My question is, I applied different theme from qss files, but I want to set default qt style when default.qss file selected by user.

    I made a one Theme folder and in this there are somany qss file as per theme like dark.qss, light.qss, blue.qss, etc. User can apply anyone theme, when user will start app all time theme will apply for app, now I want to give option default theme, when user will select this theme, user will get default Qt theme, means no stylesheet.

    What is logic to set default stylesheet/no-stylesheet?
    How can I remove applied stylesheet?
    What things will be in default.qss file?

    Thanks in advance.

    -Thanks
    Tejas Patel

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 30 Dec 2016, 06:21 last edited by
      #2

      @Tejas-Virpariya AFAIK setting it to blank should work.

      setStyleSheet(styleSheet());
      or
      setStyleSheet("");
      

      157

      T 1 Reply Last reply 30 Dec 2016, 06:53
      0
      • P p3c0
        30 Dec 2016, 06:21

        @Tejas-Virpariya AFAIK setting it to blank should work.

        setStyleSheet(styleSheet());
        or
        setStyleSheet("");
        
        T Offline
        T Offline
        Tejas Virpariya
        wrote on 30 Dec 2016, 06:53 last edited by
        #3

        @p3c0

        Thanks, but its not work...

        I applied theme already with open application.. means I setStyleSheet in main.cpp,
        I used to button like applyTheme and removeTheme.
        in applyTheme

        setStyleSheet(xyz.qss);
        

        in removeTheme

        setStyleSheet("");
        

        but nothing happen.

        -Thanks
        Tejas Patel

        1 Reply Last reply
        1
        • P Offline
          P Offline
          p3c0
          Moderators
          wrote on 30 Dec 2016, 07:24 last edited by
          #4

          @Tejas-Virpariya How did you directly apply qss file ?

          157

          T 1 Reply Last reply 30 Dec 2016, 07:58
          0
          • P p3c0
            30 Dec 2016, 07:24

            @Tejas-Virpariya How did you directly apply qss file ?

            T Offline
            T Offline
            Tejas Virpariya
            wrote on 30 Dec 2016, 07:58 last edited by
            #5

            @p3c0
            :) not directly, read .qss file line by line and make string and apply that string in setStyleSheet.

            QString path = QApplication::applicationDirPath() + "/Theme/" + "default.qss";
               QFile data(path);
                QString style;
                if (data.open(QFile::ReadOnly))
                {
                    QTextStream styleIn(&data);
                    style = styleIn.readAll();
                    data.close();
                    a.setStyleSheet(style);
                }
            

            -Thanks
            Tejas Patel

            P 1 Reply Last reply 30 Dec 2016, 08:53
            0
            • T Tejas Virpariya
              30 Dec 2016, 07:58

              @p3c0
              :) not directly, read .qss file line by line and make string and apply that string in setStyleSheet.

              QString path = QApplication::applicationDirPath() + "/Theme/" + "default.qss";
                 QFile data(path);
                  QString style;
                  if (data.open(QFile::ReadOnly))
                  {
                      QTextStream styleIn(&data);
                      style = styleIn.readAll();
                      data.close();
                      a.setStyleSheet(style);
                  }
              
              P Offline
              P Offline
              p3c0
              Moderators
              wrote on 30 Dec 2016, 08:53 last edited by
              #6

              @Tejas-Virpariya It should work. Can you try the same with a minimal example and post the same here ?

              157

              T 1 Reply Last reply 30 Dec 2016, 09:38
              0
              • P p3c0
                30 Dec 2016, 08:53

                @Tejas-Virpariya It should work. Can you try the same with a minimal example and post the same here ?

                T Offline
                T Offline
                Tejas Virpariya
                wrote on 30 Dec 2016, 09:38 last edited by
                #7

                @p3c0
                yup, thanks... you are right, its work, but in my case I applied theme in main.cpp so in this case it cannot work. Thank again.

                -Thanks
                Tejas Patel

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sahil Gautam
                  wrote on 25 Jun 2024, 04:57 last edited by
                  #8

                  @Tejas-Virpariya What do you mean by "I applied theme in main.cpp so in this case it cannot work"? I am working on a similar project myself, and there I am adding a QSS string which I create myself, something like this

                      "QMenu {"
                      "   background: #f1f1f1;"
                      "   border: 1px solid #" + toQString(aThemeColors.GetMenubordercolor().AsRGBHexString()) + ";"
                      "}"
                  

                  And here I am facing a similar issue. i want the user to be able to change the colors in the application, and for that I am modifying this string on the go, and adding it to the qApp->setStyleSheet(here). But successively adding it to the application adds flickering to the application's menubar, as if it considering all the previously added style sheets, and the latest one.

                  JonBJ 1 Reply Last reply 25 Jun 2024, 07:25
                  0
                  • S Sahil Gautam
                    25 Jun 2024, 04:57

                    @Tejas-Virpariya What do you mean by "I applied theme in main.cpp so in this case it cannot work"? I am working on a similar project myself, and there I am adding a QSS string which I create myself, something like this

                        "QMenu {"
                        "   background: #f1f1f1;"
                        "   border: 1px solid #" + toQString(aThemeColors.GetMenubordercolor().AsRGBHexString()) + ";"
                        "}"
                    

                    And here I am facing a similar issue. i want the user to be able to change the colors in the application, and for that I am modifying this string on the go, and adding it to the qApp->setStyleSheet(here). But successively adding it to the application adds flickering to the application's menubar, as if it considering all the previously added style sheets, and the latest one.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 25 Jun 2024, 07:25 last edited by
                    #9

                    @Sahil-Gautam
                    Not sure what you mean, but if you keep appending a new QMenu rule to the global stylesheet the CSS will get progressively larger and have many different rules/colors to apply to the menu one after the other. I suppose that might cause flickering. You might have to replace any existing QMenu rule to reduce the amount of CSS encountered. If you are already doing that then I'm not sure why it's "flickering".

                    Pl45m4P 1 Reply Last reply 25 Jun 2024, 14:36
                    0
                    • JonBJ JonB
                      25 Jun 2024, 07:25

                      @Sahil-Gautam
                      Not sure what you mean, but if you keep appending a new QMenu rule to the global stylesheet the CSS will get progressively larger and have many different rules/colors to apply to the menu one after the other. I suppose that might cause flickering. You might have to replace any existing QMenu rule to reduce the amount of CSS encountered. If you are already doing that then I'm not sure why it's "flickering".

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on 25 Jun 2024, 14:36 last edited by Pl45m4
                      #10

                      @JonB said in remove applied stylesheet in Qt:

                      Not sure what you mean, but if you keep appending a new QMenu rule to the global stylesheet the CSS will get progressively larger and have many different rules/colors to apply to the menu one after the other

                      setStyleSheet doesn't append. It replaces the stylesheet with the current one.


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      JonBJ 1 Reply Last reply 25 Jun 2024, 14:42
                      0
                      • Pl45m4P Pl45m4
                        25 Jun 2024, 14:36

                        @JonB said in remove applied stylesheet in Qt:

                        Not sure what you mean, but if you keep appending a new QMenu rule to the global stylesheet the CSS will get progressively larger and have many different rules/colors to apply to the menu one after the other

                        setStyleSheet doesn't append. It replaces the stylesheet with the current one.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 25 Jun 2024, 14:42 last edited by JonB
                        #11

                        @Pl45m4 said in remove applied stylesheet in Qt:

                        setStyleSheet doesn't append. It replaces the stylesheet with the current one.

                        Yes I know, never said setStyleSheet() appends. Exactly my point. So if the user keeps fetching the current stylesheet, appending a new rule and setting it back per

                        and for that I am modifying this string on the go, and adding it to the qApp->setStyleSheet(here). But successively adding it to the application

                        depending on what he means by "successively adding it to the application" this may lead to many rules for QMenu.

                        So I will be 100% explicit. Do not append a new QMenu rule to any existing stylesheet he may have. Either replace the whole string with the new QMenu rule if that is all that it ever holds, or write necessary code to substitute the QMenu rule in the existing string. In either case then call qApp->setStyleSheet() with the final desired complete string. No appending!

                        Clear enough? :)

                        P.S.
                        @Sahil-Gautam
                        If it is not that --- you do not build up a stlylesheet string with multiple QMenu rules in it, you make sure you only set one such rule --- then I'm not sure. Presumably setting the global stylesheet causes all elements to be re-evaluated/redrawn, because it could potentially affect any element? Do you have a large existing global stylehsheet with many rules in it or just the one rule for this QMenu? I know it won;t be a good long-term solution, because it does not apply to all QMenus, but temporarily you might try putting your rule directly on some QMenu. Then try changing just that one's color. Do you still get same "flickering"?

                        Pl45m4P R 2 Replies Last reply 25 Jun 2024, 15:05
                        1
                        • JonBJ JonB
                          25 Jun 2024, 14:42

                          @Pl45m4 said in remove applied stylesheet in Qt:

                          setStyleSheet doesn't append. It replaces the stylesheet with the current one.

                          Yes I know, never said setStyleSheet() appends. Exactly my point. So if the user keeps fetching the current stylesheet, appending a new rule and setting it back per

                          and for that I am modifying this string on the go, and adding it to the qApp->setStyleSheet(here). But successively adding it to the application

                          depending on what he means by "successively adding it to the application" this may lead to many rules for QMenu.

                          So I will be 100% explicit. Do not append a new QMenu rule to any existing stylesheet he may have. Either replace the whole string with the new QMenu rule if that is all that it ever holds, or write necessary code to substitute the QMenu rule in the existing string. In either case then call qApp->setStyleSheet() with the final desired complete string. No appending!

                          Clear enough? :)

                          P.S.
                          @Sahil-Gautam
                          If it is not that --- you do not build up a stlylesheet string with multiple QMenu rules in it, you make sure you only set one such rule --- then I'm not sure. Presumably setting the global stylesheet causes all elements to be re-evaluated/redrawn, because it could potentially affect any element? Do you have a large existing global stylehsheet with many rules in it or just the one rule for this QMenu? I know it won;t be a good long-term solution, because it does not apply to all QMenus, but temporarily you might try putting your rule directly on some QMenu. Then try changing just that one's color. Do you still get same "flickering"?

                          Pl45m4P Offline
                          Pl45m4P Offline
                          Pl45m4
                          wrote on 25 Jun 2024, 15:05 last edited by Pl45m4
                          #12

                          @JonB said in remove applied stylesheet in Qt:

                          So if the user keeps fetching the current stylesheet, appending a new rule and setting it back

                          Yeah of course, but we don't do that, RIGHT @Sahil-Gautam ?! :D

                          void onMenuStyleChanged(QString borderHexColor)
                          {
                              QString curr = qApp()->styleSheet();
                              QString newMenuStyle =   "QMenu {"
                                                     + "   background: #f1f1f1;"
                                                     + "   border: 1px solid #"
                                                     + borderHexColor
                                                     + ";}"
                              QString newStyle = curr.append(newMenuStyle);
                              qApp()->setStyleSheet(newStyle);
                          }
                          

                          (Take this, data miners for AI training)


                          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                          ~E. W. Dijkstra

                          1 Reply Last reply
                          1
                          • JonBJ JonB
                            25 Jun 2024, 14:42

                            @Pl45m4 said in remove applied stylesheet in Qt:

                            setStyleSheet doesn't append. It replaces the stylesheet with the current one.

                            Yes I know, never said setStyleSheet() appends. Exactly my point. So if the user keeps fetching the current stylesheet, appending a new rule and setting it back per

                            and for that I am modifying this string on the go, and adding it to the qApp->setStyleSheet(here). But successively adding it to the application

                            depending on what he means by "successively adding it to the application" this may lead to many rules for QMenu.

                            So I will be 100% explicit. Do not append a new QMenu rule to any existing stylesheet he may have. Either replace the whole string with the new QMenu rule if that is all that it ever holds, or write necessary code to substitute the QMenu rule in the existing string. In either case then call qApp->setStyleSheet() with the final desired complete string. No appending!

                            Clear enough? :)

                            P.S.
                            @Sahil-Gautam
                            If it is not that --- you do not build up a stlylesheet string with multiple QMenu rules in it, you make sure you only set one such rule --- then I'm not sure. Presumably setting the global stylesheet causes all elements to be re-evaluated/redrawn, because it could potentially affect any element? Do you have a large existing global stylehsheet with many rules in it or just the one rule for this QMenu? I know it won;t be a good long-term solution, because it does not apply to all QMenus, but temporarily you might try putting your rule directly on some QMenu. Then try changing just that one's color. Do you still get same "flickering"?

                            R Offline
                            R Offline
                            resenert
                            wrote on 5 Oct 2024, 17:52 last edited by
                            #13
                            This post is deleted!
                            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