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. QCSS precedence versus "in-line code"
Forum Updated to NodeBB v4.3 + New Features

QCSS precedence versus "in-line code"

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 3.4k Views 3 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.
  • sierdzioS sierdzio

    @JonB said in QCSS precedence versus "in-line code":

    So, in Qt, I guess I need to regard anything set via a style sheet, at any level, as overriding any corresponding thing the code may have set on the widget explicitly. Is that right?

    No, that is wrong. QSS works only for the widget you set it on + all children. It does not go up in the parent-child tree.

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

    @sierdzio said in QCSS precedence versus "in-line code":

    @JonB said in QCSS precedence versus "in-line code":

    So, in Qt, I guess I need to regard anything set via a style sheet, at any level, as overriding any corresponding thing the code may have set on the widget explicitly. Is that right?

    No, that is wrong. QSS works only for the widget you set it on + all children. It does not go up in the parent-child tree.

    For this one, I think you're misunderstanding me. I know that inheritance is only downward, not upward (remember, I'm familiar with CSS!). I didn't imply otherwise. I meant: no matter where I do set CSS --- e.g. directly on a widget, or on any level of parentage --- that is going to override any explicit, say, setMinimumHeight() calls the code might make directly on the widget, of which there are probably many to track down in the code. And it will apply to any call, not just setMinimumHeight().....

    1 Reply Last reply
    0
    • sierdzioS sierdzio

      Try adding the button to the layout first, then applying your explicit changes. I suppose what happens now is that you set up your custom widget, and then when you add it to layout, the layout is pushing it's stylesheet to that widget.

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

      @sierdzio said in QCSS precedence versus "in-line code":

      Try adding the button to the layout first, then applying your explicit changes. I suppose what happens now is that you set up your custom widget, and then when you add it to layout, the layout is pushing it's stylesheet to that widget.

      Nope! Done that (moved self.centralLayout.addWidget(self.btn2) up to immediately below self.btn2 = QtWidgets.QPushButton("Button #2")), behaviour is exactly the same, stylesheet overrides "in-line code" ... ?

      sierdzioS 1 Reply Last reply
      0
      • JonBJ JonB

        @sierdzio said in QCSS precedence versus "in-line code":

        Try adding the button to the layout first, then applying your explicit changes. I suppose what happens now is that you set up your custom widget, and then when you add it to layout, the layout is pushing it's stylesheet to that widget.

        Nope! Done that (moved self.centralLayout.addWidget(self.btn2) up to immediately below self.btn2 = QtWidgets.QPushButton("Button #2")), behaviour is exactly the same, stylesheet overrides "in-line code" ... ?

        sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #6

        @JonB said in QCSS precedence versus "in-line code":

        Nope! Done that (moved self.centralLayout.addWidget(self.btn2) up to immediately below self.btn2 = QtWidgets.QPushButton("Button #2")), behaviour is exactly the same, stylesheet overrides "in-line code" ... ?

        Looks like a bug to me, then.

        (Z(:^

        JonBJ 1 Reply Last reply
        0
        • sierdzioS sierdzio

          @JonB said in QCSS precedence versus "in-line code":

          Nope! Done that (moved self.centralLayout.addWidget(self.btn2) up to immediately below self.btn2 = QtWidgets.QPushButton("Button #2")), behaviour is exactly the same, stylesheet overrides "in-line code" ... ?

          Looks like a bug to me, then.

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

          @sierdzio
          Well, I can't see it's a "bug", as it must be a thorough-going behaviour principle. That's why I illustrated both with a size and a color.

          Is someone able to confirm what the "principle" is here, please?

          sierdzioS 1 Reply Last reply
          0
          • JonBJ JonB

            @sierdzio
            Well, I can't see it's a "bug", as it must be a thorough-going behaviour principle. That's why I illustrated both with a size and a color.

            Is someone able to confirm what the "principle" is here, please?

            sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #8

            @JonB said in QCSS precedence versus "in-line code":

            Well, I can't see it's a "bug", as it must be a thorough-going behaviour principle.

            If it is not mentioned in the documentation, it is a bug. Besides, the intuition is that when you change something explicitly, it should not be overridden by default global setting, IMO. So it feels buggy to me (esp. since I don't remember it working this way previously).

            (Z(:^

            JonBJ 1 Reply Last reply
            0
            • sierdzioS sierdzio

              @JonB said in QCSS precedence versus "in-line code":

              Well, I can't see it's a "bug", as it must be a thorough-going behaviour principle.

              If it is not mentioned in the documentation, it is a bug. Besides, the intuition is that when you change something explicitly, it should not be overridden by default global setting, IMO. So it feels buggy to me (esp. since I don't remember it working this way previously).

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

              @sierdzio
              I take your point. However it's a pretty fundamental design one way or the other, and I have refactor all code in that light now, I cannot wait till Qt might change its mind in the future. So my immediate problem is to figure out what I might need to look for in my code now which may need changing as a consequence.

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

                Hi,

                From the style sheet documentation. It’s coherent. Style sheets guarantee that what you set is what you get and only what it doesn’t handle, it forwards to the OS style.

                So depending on your needs, you might want to consider implementing your own style like Qt Creator does.

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

                JonBJ 1 Reply Last reply
                1
                • SGaistS SGaist

                  Hi,

                  From the style sheet documentation. It’s coherent. Style sheets guarantee that what you set is what you get and only what it doesn’t handle, it forwards to the OS style.

                  So depending on your needs, you might want to consider implementing your own style like Qt Creator does.

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

                  @SGaist
                  Thank you as ever for your response.

                  I have (re-)read the link, and I did not get from it that an explicit, say, QWidget::setMaximumWidth() would be overridden by a generic stylesheet max-width:. But I am not as familiar with Qt as you are.

                  So, whether I like it or not, if an application stylesheet has QPushButon { max-width: 100px; }, and that suits 90% of the buttons in the app, but dotted over the code are the occasional btn.setMaximumWidth(XXX)s with different numbers and not suitable for common stylesheet rules, they will have no effect. They would need to be changed to btn.setStyleSheet("max-width: XXXpx;") instead if I want to be sure they come into effect.

                  Once stylesheets are in use, you must use setStyleSheet() and not the dedicated Qt properties to be sure to override. Right?

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

                    Yes that's right. However, like I wrote before depending on your needs, a custom QStyle might be more suitable.

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

                    JonBJ 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Yes that's right. However, like I wrote before depending on your needs, a custom QStyle might be more suitable.

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

                      @SGaist
                      OK, thanks for confirming.

                      What link should I read up for "a custom QStyle" and "you might want to consider implementing your own style like Qt Creator does", as I don't know about this?

                      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