PDA

View Full Version : Can't change background on QWidget using stylesheets



Tottish
10th February 2011, 12:53
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.

high_flyer
10th February 2011, 16:19
Can you show the code where you set the style sheet?

Tottish
10th February 2011, 18:23
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

high_flyer
10th February 2011, 19:53
Did you override the paintEvent()?

Tottish
10th February 2011, 20:08
No, as I said I just inherited and added nothing.

wysota
10th February 2011, 21:01
http://www.qtcentre.org/threads/37976-Q_OBJECT-and-CSS-background-image

Tottish
11th February 2011, 13:30
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

wysota
11th February 2011, 14:32
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.

high_flyer
11th February 2011, 14:42
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.

Tottish
11th February 2011, 15:03
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