Results 1 to 10 of 10

Thread: Can't change background on QWidget using stylesheets

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

    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
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    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
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Sure, been trying many different versions but what's currently in there is:

    QWidget{background-color: rgb(219, 255, 255);}

    Doesn't seem to matter if I set it directly in Designer, the ctor of my custom widget or from client code. Same result.
    And as I said before it works fine for the preview but not when installed on a widget in the mainwindow.

    /Tottish


    Added after 46 minutes:


    Seems I was wrong before. It actually DOES work as I want it to when I setup the form on a standard QWidget so the problem must indeed lie in my custom widget that inherits QWidget and sets up the ui onto itself in the ctor.
    I'll look over the code.
    /Totte


    Added after 1 10 minutes:


    OK, so I've been trying to track the problem down and it seems to me that something happens when sub-classing QWidget that creates this behavior.

    What I've done is to make a new subclass with QWidget as baseclass, using the "new class-dialog" in QtCreator, and then made no changes to it. I thought that instances of this new class would be exactly the same as QWidgets but when I try to setup the ui on an instance of this new class I don't get the background to change color.

    So, what's happening when sub-classing QWidget that creates this behavior?

    /Tottish
    Last edited by Tottish; 10th February 2011 at 18:23.

  4. #4
    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: Can't change background on QWidget using stylesheets

    Did you override the paintEvent()?
    ==========================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.

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

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

    No, as I said I just inherited and added nothing.

  6. #6
    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: 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.


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

    Tottish (11th February 2011)

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

    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

  9. #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: 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.


  10. #9
    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: 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.

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

    Tottish (11th February 2011)

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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.