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. QWidget::setStyleSheet() breaks cascade inheritance
Forum Updated to NodeBB v4.3 + New Features

QWidget::setStyleSheet() breaks cascade inheritance

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 5 Posters 6.4k Views 4 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
    #21

    I just wanted to report back in case anybody thinks I am slacking....

    Spent another day of frustration :( In my real-life, I have something like hierarchy ... > QTabWidget > QScrollArea > .... Higher up sets stylesheet like QScrollBar { ... }. I am finding that setting any kind of non-empty [including nothing related to scrolls/tabs] setStyleSheet { ... } on either the QScrollArea or on its direct parent (like QTabWidget here) stops inheritance of the scrollbars. Putting in setStyleSheet { ... } anywhere else (e.g. higher up than direct parent) does not stop inheritance....

    ...But of course, try as I might, I cannot reproduce this behaviour on any kind of standalone... :( It's proving impossible to recognise what the difference is.

    One thing: I just happenstanced across @sierdzio's comment from Feb 2019 at https://forum.qt.io/topic/100083/qt-how-to-prevent-child-qdialogs-or-qwidgets-from-inheriting-parent-stylesheet/2:

    If you want to stop subwidgets from inheriting the style, call setStyleSheet("") on widgets which should have default style sheet.

    [Leave aside for the moment that I find setStyleSheet("") has no effect/does not cancel inheritance, it must be setStyleSheet(<something-here>), even if it's just " ".]

    Is @sierdzio around? I need him or somebody to give me an actual reference to where they have seen this in the docs, please? It does not seem to me things work like that, at least not in the cases I have tried (and see @kshegunov's sample code above), but there does seem to be a possible relationship between this statement and (some of) my issue, so I really need to pursue....

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #22

      Heh, yes I am around. I didn't respond earlier because I thought I had an answer... then rechecked the docs and it turned out I remembered it incorrectly.

      So, I don't have an explanation for you. Perhaps it is a Qt bug (quite probably it is, in fact!), or you are doing something wrong (seems unlikely, from what you describe). I can only offer some hints that might help:

      • do you break parent/child relationship on your widgets at any point (using QObject::setParent() or something similar)? It could break QSS inheritance chain, too
      • I have recently stumbled upon some widgets which do not work with QSS at all if they have custom QPalette set. I don't have a full list, from memory I'd say it was at least QLabel and QLineEdit, probably more. So if you do call setPalette() anywhere - this could be your problem
      • there is a way to avoid setting custom stylesheets for particular widgets. If you want some widget (let's call it WidgetA) to have different look, but you also want to have only one application-wide QSS file, then: pick some property name and set it WidgetA->setProperty("myCustomName", true);, then in your QSS file, you can create special rules only for widget(s) which have this property: QWidget[myCustomName] { background-color: #FFFFFF; border: 1px solid #E9EDEF;}. It works, and as a bonus you get all your style code in a single, global place :-)

      (Z(:^

      JonBJ 1 Reply Last reply
      0
      • sierdzioS sierdzio

        Heh, yes I am around. I didn't respond earlier because I thought I had an answer... then rechecked the docs and it turned out I remembered it incorrectly.

        So, I don't have an explanation for you. Perhaps it is a Qt bug (quite probably it is, in fact!), or you are doing something wrong (seems unlikely, from what you describe). I can only offer some hints that might help:

        • do you break parent/child relationship on your widgets at any point (using QObject::setParent() or something similar)? It could break QSS inheritance chain, too
        • I have recently stumbled upon some widgets which do not work with QSS at all if they have custom QPalette set. I don't have a full list, from memory I'd say it was at least QLabel and QLineEdit, probably more. So if you do call setPalette() anywhere - this could be your problem
        • there is a way to avoid setting custom stylesheets for particular widgets. If you want some widget (let's call it WidgetA) to have different look, but you also want to have only one application-wide QSS file, then: pick some property name and set it WidgetA->setProperty("myCustomName", true);, then in your QSS file, you can create special rules only for widget(s) which have this property: QWidget[myCustomName] { background-color: #FFFFFF; border: 1px solid #E9EDEF;}. It works, and as a bonus you get all your style code in a single, global place :-)
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #23

        @sierdzio
        Hi there, thanks for replying :)

        • No reparenting, nothing going on in code like that (bit I will recheck tomorrow). The layout is designed from Creator, not manipulated in our code.
        • Nothing like palettes or special widgets.
        • Yep, I did dynamic properties in my previous project. Not at issue here. I have done a lot of CSS for my sinds, so I do know what it's all about.

        So can we be clear, please? Are you withdrawing your:

        If you want to stop subwidgets from inheriting the style, call setStyleSheet("") on widgets which should have default style sheet.

        I have not seen Qt says it would do that. Unless it says it does I would not expect it to --- it would break the simplest case of defining a global set of styles and then occasionally doing one thing on an element if that cancelled all inheritance. My "standalone" tests do not reveal such behaviour.

        Yet my problem cases are behaving as though that does happen, in unrepeatable circumstances. That is why I ask. Something seems to be going "if such and such an element has any stylesheet of is own, cancel inheritance from its ancestors at this point and down to descendants". When it should not.

        As I have said elsewhere, it may have something to do with QTabWidgets & QScrollBars. I have already discovered that "containers" like these do not behave as one would expect for inheritance, I may make a separate post tomorrow showing that. EDIT See https://forum.qt.io/topic/111137/stylesheet-inheritance-and-container-widgets.

        1 Reply Last reply
        0
        • JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #24

          I have raised https://bugreports.qt.io/browse/QTBUG-81958 against related https://forum.qt.io/topic/111137/stylesheet-inheritance-and-container-widgets, as I'm not getting an answer here.

          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