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. Undoing an added "setStyleSheet" property
Forum Updated to NodeBB v4.3 + New Features

Undoing an added "setStyleSheet" property

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 5 Posters 5.2k Views 2 Watching
  • 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.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #1
    • I inherit from a class whose constructor includes someWidget.setStyleSheet("background-color: transparent;")
    • For my derived class I do not want to alter the widget's background color.
    • After the base class constructor has been called, I can't/don't want to just go someWidget.setStyleSheet("") to undo that, because for all I know there may have been something else in the setStyleSheet() call with other styles which I don't want to affect. (So I find solution https://forum.qt.io/topic/21045/solved-how-to-restore-qtextedit-original-background-color unacceptable.)
    • I am used to HTML/CSS/JS, where I could access and alter an individual "backgroundColor" property, but I don't see that from Qt CSS.
    • So all I can see is:
    styleSheet = someWidget.styleSheet()
    styleSheet = styleSheet.regularExpressionReplace("background-color: ...", "")
    someWidget.setStyleSheet(styleSheet)
    

    which is all a bit ugly.

    Is there a better way?

    P.S.
    The widget in this case is a QTextEdit, though really the question might be aimed at any widget type, but I suppose I'll accept whatever solution is best in this particular case if necessary.

    J.HilkJ 1 Reply Last reply
    0
    • JonBJ JonB
      • I inherit from a class whose constructor includes someWidget.setStyleSheet("background-color: transparent;")
      • For my derived class I do not want to alter the widget's background color.
      • After the base class constructor has been called, I can't/don't want to just go someWidget.setStyleSheet("") to undo that, because for all I know there may have been something else in the setStyleSheet() call with other styles which I don't want to affect. (So I find solution https://forum.qt.io/topic/21045/solved-how-to-restore-qtextedit-original-background-color unacceptable.)
      • I am used to HTML/CSS/JS, where I could access and alter an individual "backgroundColor" property, but I don't see that from Qt CSS.
      • So all I can see is:
      styleSheet = someWidget.styleSheet()
      styleSheet = styleSheet.regularExpressionReplace("background-color: ...", "")
      someWidget.setStyleSheet(styleSheet)
      

      which is all a bit ugly.

      Is there a better way?

      P.S.
      The widget in this case is a QTextEdit, though really the question might be aimed at any widget type, but I suppose I'll accept whatever solution is best in this particular case if necessary.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @JNBarchan Hi, why don't you give your baseclass an object name and referece that in your stylesheet, that way in your deriufec widget you simply rename the widget

      something along the line of:

      //Base Class
      this->setObjectName("BaseWidget");
      this->setStyleSheet("QWidget#BaseWidget{background-color:white;....}");
      
      //Later in your subclass
      this->setObjectName("SomeOtherName");
      

      that should work.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      JonBJ 1 Reply Last reply
      0
      • J.HilkJ J.Hilk

        @JNBarchan Hi, why don't you give your baseclass an object name and referece that in your stylesheet, that way in your deriufec widget you simply rename the widget

        something along the line of:

        //Base Class
        this->setObjectName("BaseWidget");
        this->setStyleSheet("QWidget#BaseWidget{background-color:white;....}");
        
        //Later in your subclass
        this->setObjectName("SomeOtherName");
        

        that should work.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @J.Hilk
        Because:

        1. I have no desire to change the base class code, else I would have said so.
        2. For all I know, the base class might go:
          this->setStyleSheet("QWidget#BaseWidget{background-color:white; another-property: something; ...}");
          and I will want any other style properties, just not the background color, as I said.

        The question is: how to remove a single property which may have been set in a previous setStyleSheet(...) call?

        P.S.
        I've just realised, it's not just "remove" a single property, it's "override" a single property, which might or might not have been set. In this case, I'm willing to set background color to lightgrey instead of removing it, but it still leaves me with the issue of having to parse & change the whole of the existing string in case other properties might have been set....

        J.HilkJ 1 Reply Last reply
        0
        • JonBJ JonB

          @J.Hilk
          Because:

          1. I have no desire to change the base class code, else I would have said so.
          2. For all I know, the base class might go:
            this->setStyleSheet("QWidget#BaseWidget{background-color:white; another-property: something; ...}");
            and I will want any other style properties, just not the background color, as I said.

          The question is: how to remove a single property which may have been set in a previous setStyleSheet(...) call?

          P.S.
          I've just realised, it's not just "remove" a single property, it's "override" a single property, which might or might not have been set. In this case, I'm willing to set background color to lightgrey instead of removing it, but it still leaves me with the issue of having to parse & change the whole of the existing string in case other properties might have been set....

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @JNBarchan Mhh,than I don't see an other way than that what you described in the OP, or something very similar.

          Maybe someone else has a better idea.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • jazzycamelJ Offline
            jazzycamelJ Offline
            jazzycamel
            wrote on last edited by
            #5

            I don't know if the CSS 'initial' and/or 'unset' values work with Qt stylesheets, might be worth a try? For example:

            setStyleSheet("background-color: initial;");
            

            or

            setStyleSheet("background-color: unset;");
            

            For the avoidance of doubt:

            1. All my code samples (C++ or Python) are tested before posting
            2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
            JonBJ 1 Reply Last reply
            2
            • jazzycamelJ jazzycamel

              I don't know if the CSS 'initial' and/or 'unset' values work with Qt stylesheets, might be worth a try? For example:

              setStyleSheet("background-color: initial;");
              

              or

              setStyleSheet("background-color: unset;");
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @jazzycamel
              This looked good, and is a reasonable answer to the question!

              However, in my case both of these resulted in the QTextEdit's going black, which is not good given that the text on it is black too :) I'm guessing that somewhere else (like the app's global stylesheet which the code reads in and applies) there is some default entry which sets all QTextEdits' default background color to something else, and initial/unset reverts to some other base, default color, losing that :(

              So a possible good answer, but in my case it really does look like I need to either remove or alter the background-color (only) explicitly on the widget...?

              1 Reply Last reply
              0
              • jazzycamelJ Offline
                jazzycamelJ Offline
                jazzycamel
                wrote on last edited by
                #7

                Thats a shame, maybe consider raising a feature request for that one.

                Seems to me that saving and restoring the original stylesheet might be the only way to then.

                For the avoidance of doubt:

                1. All my code samples (C++ or Python) are tested before posting
                2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
                JonBJ 1 Reply Last reply
                0
                • jazzycamelJ jazzycamel

                  Thats a shame, maybe consider raising a feature request for that one.

                  Seems to me that saving and restoring the original stylesheet might be the only way to then.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @jazzycamel
                  Yep, at present that seems to be the only route.

                  setStyleSheet principle could do with a couple of enhancements:

                  1. appendToStyleSheet(str). Adds some more stuff to the existing stylesheet without requiring you to fetch its current content and then reset the whole thing. Having said that, there are calls to fetch->change->settonew, so it's only really a convenience.

                  2. From a setStyleSheet, I assume that at some level Qt parses the properties string into its constituent properties to set. Make it so the individual properties can then be accessed directly, e.g. QWidget::setStyleProperty("background-color", newValue) or QWidget::removeStyleProperty("background-color").

                  At the moment, because there's only a setStyleSheet, the code I have inherited is full of code in different modules using this, and who knows whether that will overwrite other deliberate changes from elsewhere. It doesn't lend itself to safe, self-contained programming features.

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    Vadi2
                    wrote on last edited by
                    #9

                    Chances of this happening, apart from a contribution to Qt, are pretty slim though. The roadmap is currently fascinated with QML, customising QtWidgets is on the wayside.

                    JonBJ 1 Reply Last reply
                    0
                    • Vinod KuntojiV Offline
                      Vinod KuntojiV Offline
                      Vinod Kuntoji
                      wrote on last edited by
                      #10

                      @JNBarchan ,
                      To undo the stylesheet, you can use
                      textEdit->setStylesheet("");

                      C++, Qt, Qt Quick Developer,
                      PthinkS, Bangalore

                      JonBJ 1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        Vadi2
                        wrote on last edited by
                        #11

                        That would undo the whole stylesheet - not what the topic is looking for. OP is interested in undoing 1 property.

                        1 Reply Last reply
                        1
                        • Vinod KuntojiV Vinod Kuntoji

                          @JNBarchan ,
                          To undo the stylesheet, you can use
                          textEdit->setStylesheet("");

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #12

                          @Vinod-Kuntoji
                          As @Vadi2 has just said, the whole point of the question is how to selectively modify certain individual property assignments which may have been made previously, without altering any others.

                          1 Reply Last reply
                          0
                          • V Vadi2

                            Chances of this happening, apart from a contribution to Qt, are pretty slim though. The roadmap is currently fascinated with QML, customising QtWidgets is on the wayside.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #13

                            @Vadi2 said in Undoing an added "setStyleSheet" property:

                            Chances of this happening, apart from a contribution to Qt, are pretty slim though. The roadmap is currently fascinated with QML, customising QtWidgets is on the wayside.

                            Although I am a total noob to Qt, I read yesterday (somewhere...) that actually the roadmap now is to sideline development on QML, because it has received so much attention, and concentrate on new features for Widgets, to bring them back up to speed!

                            1 Reply Last reply
                            1
                            • Vinod KuntojiV Offline
                              Vinod KuntojiV Offline
                              Vinod Kuntoji
                              wrote on last edited by
                              #14

                              @Vadi2 ,@JNBarchan

                              Ya, I can undo only the 1 property without altering others. I hope you got the solution.

                              ui->textEdit->setStyleSheet("background-color:red;font-size:12pt;");

                              ui->textEdit->setStyleSheet("background-color:none;font-size:12pt;");

                              C++, Qt, Qt Quick Developer,
                              PthinkS, Bangalore

                              JonBJ 1 Reply Last reply
                              0
                              • Vinod KuntojiV Vinod Kuntoji

                                @Vadi2 ,@JNBarchan

                                Ya, I can undo only the 1 property without altering others. I hope you got the solution.

                                ui->textEdit->setStyleSheet("background-color:red;font-size:12pt;");

                                ui->textEdit->setStyleSheet("background-color:none;font-size:12pt;");

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #15

                                @Vinod-Kuntoji
                                That does not just alter the background-color property, as I might want. That alters the whole of the stylesheet, and relies on knowledge that font-size has been set to 12pt to set that too, information which is not available to me in the generic case I have described (e.g. some other piece of code somewhere has set it, which I would not be aware of). If you read what I have asked you will see this is explained, as others understand.

                                Furthermore (I think), background-color:none does not "undo" background-color:red: it explicitly sets it to "none", which will override the color from elsewhere, whereas in this case what I wish to do is remove the explicit specification of any background-color on the widget. However, that is by-the-by to the central point described in the paragraph above.

                                1 Reply Last reply
                                0
                                • JonBJ JonB referenced this topic on

                                • Login

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