Results 1 to 10 of 10

Thread: Can't change background on QWidget using stylesheets

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2010
    Posts
    77
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Can't change background on QWidget using stylesheets

    Hi!
    I'm using a custom widget that I've built inheriting from QWidget. The UI is made in designer. Just a couple of sliders, buttons and checkboxes. No layouts.

    When I apply a stylesheet inside QtDesigner (the one integerated in Creator) to change the background color of the form it works as expected and when I Preview the form it also looks as it should. But when I setup the UI onto my custom Widget (that I am putting in a VBox layout in the mainwindow) only the child widgets of the form have the right background color and not the space in between them which is transparent.

    The same thing happens when I setup the UI on a regular QWidget that I put in the main window inside or outside a layout so I don't think there is a problem with my custom QWidget.

    Can anyone tell me why this is?

    Thank you for your time!
    /Tottish

    EDIT: It works fine changing the bg-color if I'm using the Pallette.

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

    Default Re: Can't change background on QWidget using stylesheets

    Can you show the code where you set the style sheet?
    ==========================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.

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

    Default Re: Can't change background on QWidget using stylesheets

    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.


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

    Tottish (11th February 2011)

  5. #4
    Join Date
    Mar 2010
    Posts
    77
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Can't change background on QWidget using stylesheets

    Thanks a lot wysota! When reimplementing the paintEvent it worked just fine.
    One little problem, though: I can't seem to use .QWidget in the stylesheet definition to only appoint the parent-widget the specific color but it works fine when removing the dot. I guess this can be overridden by setting individual stylesheets for the child widgets but maybe it's a quick fix?

    Also: out of curiosity. Why does one have to reimplement the paint-event? As I said earlier I was under the impression that when sub-classing a class without making any changes to the sub-class, one was basically making a copy of it. However this doesn't seem to be the case here...

    Thanks again wysota! (and high_flyer)
    *TwoThumbs up*
    /Tottish

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Can't change background on QWidget using stylesheets

    Quote Originally Posted by Tottish View Post
    Thanks a lot wysota! When reimplementing the paintEvent it worked just fine.
    One little problem, though: I can't seem to use .QWidget in the stylesheet definition to only appoint the parent-widget the specific color but it works fine when removing the dot. I guess this can be overridden by setting individual stylesheets for the child widgets but maybe it's a quick fix?
    The dot says the widget has to be of type QWidget and your widget is not type of QWidget but rather its subclass. Without the dot the selector means "QWidget or descendant".


    Also: out of curiosity. Why does one have to reimplement the paint-event? As I said earlier I was under the impression that when sub-classing a class without making any changes to the sub-class, one was basically making a copy of it. However this doesn't seem to be the case here...
    The Q_OBJECT macro is responsible for this. I don't have time to dive into Qt source code right now but somewhere (probably in QWidget's paint event) there is a check if the current widget is a QWidget and only then the PE_Widget element is drawn. Since the macro is present in the class, the class is not a bare QWidget anymore.
    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.


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

    Default Re: Can't change background on QWidget using stylesheets

    can't seem to use .QWidget in the stylesheet definition to only appoint the parent-widget the specific color but it works fine when removing the dot.
    the dot is for subclasses, not children.
    Use '>' for direct children or space for descendants that are not necessarily direct children.
    ==========================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.

  8. The following user says thank you to high_flyer for this useful post:

    Tottish (11th February 2011)

  9. #7
    Join Date
    Mar 2010
    Posts
    77
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Can't change background on QWidget using stylesheets

    Aaaaah! I see...
    Did not think about the sub-class not actually being a QWidget. I smiply changed it to .MyWidgetClass and everything's running smooth now!

    Thank you for that specific info high_flyer. Obviously I had misunderstood the concept of the dot.

    Can't thank you enough, guys!
    Have a wonderful day!
    /Tottish

Similar Threads

  1. Replies: 5
    Last Post: 21st July 2010, 22:51
  2. stylesheets and trasparent background
    By godlike_panos in forum Qt Programming
    Replies: 1
    Last Post: 28th June 2010, 13:05
  3. QStandardItem background color with Stylesheets enabled
    By hubbobubbo in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2010, 10:23
  4. background gradient with stylesheets
    By martinn in forum Qt Programming
    Replies: 6
    Last Post: 28th February 2010, 15:37
  5. QWidget stylesheets not right in runtime
    By Denarius in forum Qt Programming
    Replies: 0
    Last Post: 4th June 2009, 11:21

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
  •  
Qt is a trademark of The Qt Company.