Results 1 to 8 of 8

Thread: How to stop child widgets from inheriting parent's StyleSheet and use system default?

  1. #1
    Join Date
    Jul 2007
    Posts
    56
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How to stop child widgets from inheriting parent's StyleSheet and use system default?

    I have a QDialog that I use a stylesheet to set the background color to black.
    e.g.
    QDialog {background-color: black; }

    However when I display a QMessageBox called from that dialog it inherits the stylesheet from the parent.

    I would like to be able to revert to the default stylesheet of the QMessageBox to get the background color back to normal for the QMessageBox, but can't find a way to do so. I've tried setting the stylesheet to:
    QDialog { }
    and
    QMessageBox {}
    and that doesn't work. I can set the background to another color implicitly, e.g.:
    "QDialog { background-color: white;}"
    , but want to use the default for the platform.

    Any suggestions on how to do this? Is there a way to reset a particular style back to the default instead of what it inherited from it's parent?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to stop child widgets from inheriting parent's StyleSheet and use system defa

    Try ".QDialog" instead of "QDialog". If you have a subclass of QDialog, just use its name instead of QDialog.

  3. The following user says thank you to wysota for this useful post:

    will49 (13th June 2008)

  4. #3
    Join Date
    Dec 2007
    Posts
    14
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to stop child widgets from inheriting parent's StyleSheet and use system defa

    Just name your object with ->setObjectName("myObjectName") then use object name selector # in the css.

    #myObjectName {

    }

    This has to be done on the parent dialog to prevent css inheritance, from what I know, there's no way to reset the css properties from an object that implicit inherit them.

  5. The following 2 users say thank you to benacler for this useful post:

    Big_Stone88 (16th August 2017), katte (20th December 2010)

  6. #4
    Join Date
    Jul 2007
    Posts
    56
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to stop child widgets from inheriting parent's StyleSheet and use system defa

    Thanks guys. I ended up using the name of my widget for the stylesheet and that worked OK.

  7. #5
    Join Date
    Dec 2010
    Location
    Ukraine, Kharkiv
    Posts
    17
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question How to stop child widgets inheriting parent's StyleSheet and use system default

    How to do the same in DESIGN MODE?

  8. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to stop child widgets inheriting parent's StyleSheet and use system default

    What do you mean?
    The style sheet is not aware in which "mode" you are.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #7
    Join Date
    Dec 2010
    Location
    Ukraine, Kharkiv
    Posts
    17
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to stop child widgets inheriting parent's StyleSheet and use system default

    I have Frame. I drop Button on it.
    Now I want Frame to have border-style: solid und border-width: 5px. I click on Frame->Change styleSheet... and write it there. I have what I want. Frame have solid border with 5px width. BUT, button have the same.
    Ok.
    Now I want button to have it's DEFAULT values for border-style and border-width. I want for this button to switch off StyleSheet inheritance.

  10. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to stop child widgets inheriting parent's StyleSheet and use system default

    No, you don't. You want the stylesheet to apply to one particular widget. Use a selector that will apply to this widget and not to the button, for example:
    css Code:
    1. .QFrame { border-style: solid; border-width: 5px; }
    To copy to clipboard, switch view to plain text mode 
    ... or ...
    css Code:
    1. #myFrameName { border-style: solid; border-width: 5px; }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    Chiz (13th January 2011)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.