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. is it possible to turn off stylesheet distribution?
Qt 6.11 is out! See what's new in the release blog

is it possible to turn off stylesheet distribution?

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 5.0k 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.
  • D Offline
    D Offline
    django.Reinhard
    wrote on last edited by
    #1

    Hi,

    I was preparing a screenshot for a different question, when I was shocked by this malfunction:
    FE_styles05.jpg

    I just wanted to change background color of container widget (the notebook page in this case):
    FE_styles09.jpg
    and was shocked again. WTF!

    I tried to change autofillbackground property on lineedits, but that did not change anything.
    On searching the forum for similar issues I found this answer:

    @Bonnie said in Weird stylesheet behavior for ui form designer:

    The styleSheet you set on a widget will also apply to all his children.

    What a stupid feature :(
    It changes background color of all lineedits and as shown above of a messagebox as well. What a pain.

    How can I stop distribution of stylesheet to all children?

    Pl45m4P 1 Reply Last reply
    0
    • D django.Reinhard

      Hi,

      I was preparing a screenshot for a different question, when I was shocked by this malfunction:
      FE_styles05.jpg

      I just wanted to change background color of container widget (the notebook page in this case):
      FE_styles09.jpg
      and was shocked again. WTF!

      I tried to change autofillbackground property on lineedits, but that did not change anything.
      On searching the forum for similar issues I found this answer:

      @Bonnie said in Weird stylesheet behavior for ui form designer:

      The styleSheet you set on a widget will also apply to all his children.

      What a stupid feature :(
      It changes background color of all lineedits and as shown above of a messagebox as well. What a pain.

      How can I stop distribution of stylesheet to all children?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @django-Reinhard

      Use Stylesheet selectors to control the inheritance of your stylesheet

      • https://doc.qt.io/qt-5/stylesheet-syntax.html#selector-types

      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
      3
      • D Offline
        D Offline
        django.Reinhard
        wrote on last edited by
        #3

        Hi,

        thank you very much for the pointer!

        After all it was not easy at all.
        My manager classname did not work, so I thought, may be scrollarea? But that did not work either. Then I remembered that scrollarea is just a container for a widget with my flowlayout ...
        ... but that widget is just a pure QWidget instance.

        Then I discovered, that objectName can be used as selector - and that worked :)
        FE_styles13.jpg

        Thank you for your support!

        Pl45m4P 1 Reply Last reply
        2
        • D django.Reinhard

          Hi,

          thank you very much for the pointer!

          After all it was not easy at all.
          My manager classname did not work, so I thought, may be scrollarea? But that did not work either. Then I remembered that scrollarea is just a container for a widget with my flowlayout ...
          ... but that widget is just a pure QWidget instance.

          Then I discovered, that objectName can be used as selector - and that worked :)
          FE_styles13.jpg

          Thank you for your support!

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @django-Reinhard

          Every widget can be addressed by its own objectname.

          So QScrollArea#myScrollArea would only affect the QScrollArea myScrollArea and no QPushButton inside.


          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
          • D Offline
            D Offline
            django.Reinhard
            wrote on last edited by django.Reinhard
            #5

            Yo, I got that by now. The point is that no widget has an objectName by default.

            Anyway - I don't like the top-down legislation. Never liked it.
            It's like your politician deciding that all men must wear yellow pants and red shirts - with the consequence that only yellow pants and red shirts are available for purchase.

            What I absolutely hate (!) is that you can't even intentionally override the parents' stylesheet (both without selectors).
            Such a thing is completely unacceptable to me. Yes, I know, I'm a contrarian

            JKSHJ 1 Reply Last reply
            0
            • D django.Reinhard

              Yo, I got that by now. The point is that no widget has an objectName by default.

              Anyway - I don't like the top-down legislation. Never liked it.
              It's like your politician deciding that all men must wear yellow pants and red shirts - with the consequence that only yellow pants and red shirts are available for purchase.

              What I absolutely hate (!) is that you can't even intentionally override the parents' stylesheet (both without selectors).
              Such a thing is completely unacceptable to me. Yes, I know, I'm a contrarian

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by JKSH
              #6

              @django-Reinhard said in is it possible to turn off stylesheet distribution?:

              I don't like the top-down legislation. Never liked it.
              It's like your politician deciding that all men must wear yellow pants and red shirts - with the consequence that only yellow pants and red shirts are available for purchase.

              That is the design of Cascading Style Sheets. -- A parent's style is inherited by its children, but you can still override it with a different stylesheet for the children.

              If you want to change the design, you can start a discussion with the CSS Working Group: https://github.com/w3c/csswg-drafts.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              1
              • D Offline
                D Offline
                django.Reinhard
                wrote on last edited by
                #7

                Hi,

                my html-knowledge is quite outdated, but if I remember well, a toplevel stylesheet did not overwrite a style-property used at a div, or whatever element (of cause without selectors).

                Maybe that changed - don't know.

                ... any way: I won't change Qt. This project is a challenge for me, so I just use it and I have to live with the quirks and issues.

                I am spoiled by java, which has solved many things much more elegantly than Qt. I can't remember, that I ever had such shocking impressions than with using Qt, where I already had some.

                C JKSHJ 2 Replies Last reply
                0
                • D django.Reinhard

                  Hi,

                  my html-knowledge is quite outdated, but if I remember well, a toplevel stylesheet did not overwrite a style-property used at a div, or whatever element (of cause without selectors).

                  Maybe that changed - don't know.

                  ... any way: I won't change Qt. This project is a challenge for me, so I just use it and I have to live with the quirks and issues.

                  I am spoiled by java, which has solved many things much more elegantly than Qt. I can't remember, that I ever had such shocking impressions than with using Qt, where I already had some.

                  C Offline
                  C Offline
                  ChrisW67
                  wrote on last edited by
                  #8

                  @django-Reinhard said in is it possible to turn off stylesheet distribution?:

                  but if I remember well, a toplevel stylesheet did not overwrite a style-property used at a div, or whatever element (of cause without selectors).
                  Maybe that changed - don't know.

                  Your desktop application is not a web page. This is Qt Style Sheets, or QSS. It has some similarities to CSS but it is not CSS.

                  Yo, I got that by now. The point is that no widget has an objectName by default.

                  If you are using Qt Designer standalone or embedded in Qt Creator then every widget object gets an object name by default. It matches the name you see in the form object tree (and you can change those). Here is the code generated by uic for a simple combo box I dropped in the form.

                          comboBox = new QComboBox(Form);
                          comboBox->setObjectName(QString::fromUtf8("comboBox"));
                  

                  If you are hand coding the forms then you can set a object name if you want, or need, to so that QSS has a target you can use.

                  1 Reply Last reply
                  0
                  • D django.Reinhard

                    Hi,

                    my html-knowledge is quite outdated, but if I remember well, a toplevel stylesheet did not overwrite a style-property used at a div, or whatever element (of cause without selectors).

                    Maybe that changed - don't know.

                    ... any way: I won't change Qt. This project is a challenge for me, so I just use it and I have to live with the quirks and issues.

                    I am spoiled by java, which has solved many things much more elegantly than Qt. I can't remember, that I ever had such shocking impressions than with using Qt, where I already had some.

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #9

                    @django-Reinhard said in is it possible to turn off stylesheet distribution?:

                    my html-knowledge is quite outdated, but if I remember well, a toplevel stylesheet did not overwrite a style-property used at a div, or whatever element (of cause without selectors).
                    Maybe that changed - don't know.

                    Sure it does.

                    <html>
                    <body>
                    	<div style="color:red;">
                    		I'm red.
                    		<div>I'm red too, 'cos my parent is red.</div>
                    		<div style="color:blue;">I'm blue, 'cos I don't want to be like my parent.</div>
                    	</div>
                    </body>
                    </html>
                    

                    Widgets are analogous to the "div"s here.

                    I just use it and I have to live with the quirks and issues.

                    I am spoiled by java, which has solved many things much more elegantly than Qt. I can't remember, that I ever had such shocking impressions than with using Qt, where I already had some.

                    Although @ChrisW67 is right that QSS is not CSS, Qt's style inheritance rules are fully consistent with CSS here. And CSS is used for websites all around the world, so it really shouldn't be so shocking.

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    3
                    • D Offline
                      D Offline
                      django.Reinhard
                      wrote on last edited by
                      #10

                      @JKSH said in is it possible to turn off stylesheet distribution?:

                      Widgets are analogous to the "div"s here.

                      Well, from what I tested, they are not.

                      Your sample with the divs is what I believed in.
                      But Qt behave different.

                      First I set background color to FixtureManager, which is the notebook-page. Messagebox, child-editors and QLineEdits are all children of FixtureManager - so all changed to green.

                      Then I set background-color of QLineEdit to white, but they stayed green. I tried to change autofillBackground, but it made no difference.
                      Then I tried the same for the child editor. But setting backgroundcolor was not taken into account. All stayed green.
                      That was the reason why I was shocked!

                      The div sample is ok and if Qt would behave the same, everything would be fine.

                      JonBJ JKSHJ 2 Replies Last reply
                      0
                      • D django.Reinhard

                        @JKSH said in is it possible to turn off stylesheet distribution?:

                        Widgets are analogous to the "div"s here.

                        Well, from what I tested, they are not.

                        Your sample with the divs is what I believed in.
                        But Qt behave different.

                        First I set background color to FixtureManager, which is the notebook-page. Messagebox, child-editors and QLineEdits are all children of FixtureManager - so all changed to green.

                        Then I set background-color of QLineEdit to white, but they stayed green. I tried to change autofillBackground, but it made no difference.
                        Then I tried the same for the child editor. But setting backgroundcolor was not taken into account. All stayed green.
                        That was the reason why I was shocked!

                        The div sample is ok and if Qt would behave the same, everything would be fine.

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by
                        #11

                        @django-Reinhard
                        Then maybe your code is wrong, we can't see it. More specific rules should override more generic rules in QSS, like in CSS.

                        D 1 Reply Last reply
                        0
                        • D django.Reinhard

                          @JKSH said in is it possible to turn off stylesheet distribution?:

                          Widgets are analogous to the "div"s here.

                          Well, from what I tested, they are not.

                          Your sample with the divs is what I believed in.
                          But Qt behave different.

                          First I set background color to FixtureManager, which is the notebook-page. Messagebox, child-editors and QLineEdits are all children of FixtureManager - so all changed to green.

                          Then I set background-color of QLineEdit to white, but they stayed green. I tried to change autofillBackground, but it made no difference.
                          Then I tried the same for the child editor. But setting backgroundcolor was not taken into account. All stayed green.
                          That was the reason why I was shocked!

                          The div sample is ok and if Qt would behave the same, everything would be fine.

                          JKSHJ Offline
                          JKSHJ Offline
                          JKSH
                          Moderators
                          wrote on last edited by JKSH
                          #12

                          @django-Reinhard said in is it possible to turn off stylesheet distribution?:

                          The div sample is ok and if Qt would behave the same, everything would be fine.

                          Everything is fine then:

                          auto topLevel = new QWidget;
                          auto layout = new QVBoxLayout;
                          auto greenChild = new QLineEdit("Green like parent", topLevel);
                          auto whiteChild = new QLineEdit("White like a boss", topLevel);
                          
                          topLevel->setStyleSheet("background-color: green;");
                          whiteChild->setStyleSheet("background-color: white;");
                          
                          layout->addWidget(greenChild);
                          layout->addWidget(whiteChild);
                          topLevel->setLayout(layout);
                          topLevel->show();
                          

                          Tested on Qt 5.15.2, MinGW 64-bit.

                          @Bonnie said in Weird stylesheet behavior for ui form designer:

                          The styleSheet you set on a widget will also apply to all his children.

                          What a stupid feature :(

                          Bonnie was talking about how greenChild inherits the green background from its parent.

                          As you can see, whiteChild can definitely be white, even if its parent is green.

                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                          1 Reply Last reply
                          0
                          • JonBJ JonB

                            @django-Reinhard
                            Then maybe your code is wrong, we can't see it. More specific rules should override more generic rules in QSS, like in CSS.

                            D Offline
                            D Offline
                            django.Reinhard
                            wrote on last edited by
                            #13

                            @JonB said in is it possible to turn off stylesheet distribution?:

                            Then maybe your code is wrong

                            Well, that may be true.

                            I'm not that fit with Qt that I could state, that I don't make errors.
                            I tested it with different widgets at different "distance" from parent and I got always the same result - every thing was green.

                            So if it was my fault, I beg your pardon.

                            JonBJ 1 Reply Last reply
                            0
                            • D django.Reinhard

                              @JonB said in is it possible to turn off stylesheet distribution?:

                              Then maybe your code is wrong

                              Well, that may be true.

                              I'm not that fit with Qt that I could state, that I don't make errors.
                              I tested it with different widgets at different "distance" from parent and I got always the same result - every thing was green.

                              So if it was my fault, I beg your pardon.

                              JonBJ Online
                              JonBJ Online
                              JonB
                              wrote on last edited by
                              #14

                              @django-Reinhard I just meant, we'd have to see the code just to be sure.

                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                django.Reinhard
                                wrote on last edited by
                                #15

                                Hi,

                                never mind!

                                I'll learn the Qt-way of coding and it wasn't my last error ;)

                                JKSHJ 1 Reply Last reply
                                0
                                • D django.Reinhard

                                  Hi,

                                  never mind!

                                  I'll learn the Qt-way of coding and it wasn't my last error ;)

                                  JKSHJ Offline
                                  JKSHJ Offline
                                  JKSH
                                  Moderators
                                  wrote on last edited by
                                  #16

                                  @django-Reinhard said in is it possible to turn off stylesheet distribution?:

                                  I'll learn the Qt-way of coding

                                  Did you try the code in my last message?

                                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                  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