1

I have a widget in the main window, the widget has the following styles:

    background-color:#ffeeeeee;width:100%;height:100%;

It is set-up to widget layout and controls auto size to fill the geometry of the widget, but the background of the widget is always transparent. What can I do to make it opaque?

This is using Qt5.6 running on RedHat Linux 7.2

NOTE: The syntax #ffeeeeee is specific to Qt, where the first node is the alpha, then red, green and blue octets.

SPlatten
  • 5,334
  • 11
  • 57
  • 128
  • Have you tried `setAutoFillBackground(true)`? I think it's false by default so the background won't be drawn before the `paintEvent` member is invoked. – G.M. Jul 13 '16 at 11:14
  • Yes, no difference, I also added a call to update but its still transparent. – SPlatten Jul 13 '16 at 11:16
  • Is it opaque when you write `background-color: gray;`? – ilotXXI Jul 13 '16 at 12:13
  • Regardless of the style sheet setting, the background is always transparent. – SPlatten Jul 13 '16 at 12:13
  • Please write how do you set the style. – ilotXXI Jul 13 '16 at 12:15
  • In QtCreator, select the background of the widget, right click and select "Change stylesheet..." then type in the style sheet as above, I've also tried "background-color: white;" background is always transparent. – SPlatten Jul 13 '16 at 12:17
  • Is it transparent only in QtDesigner or it is in running program too? And... what is transparency in your case, how do you see it? – ilotXXI Jul 13 '16 at 12:24
  • Its actually the other way around, its only transparent when running but opaque in the designer. – SPlatten Jul 13 '16 at 12:29

3 Answers3

1

You have too many characters in your color. Change:

background-color: #ffeeeeee;

to:

background-color: #eeeeee;

If you want to use transparency, you'll have to be explicit:

background-color: rgba(...);
jonspaceharper
  • 4,207
  • 2
  • 22
  • 42
1

With the background-color present and set to any of the following:

    background-color:white;width:100%;height:100%;

or

    background-color:#ffffff;width:100%;height:100%;

or my original:

    background-color:#ffeeeeee;width:100%;height:100%;

The property, autoFillBackground is set to false and the background of the widget is transparent at run-time.

However removing any background-color style and leaving the style as just:

    width:100%;height:100%

The property, autoFillBackground is set to true and the widget is opaque.

As far as I can see there is no error in the styles, I believe this may be an error in Qt.

SPlatten
  • 5,334
  • 11
  • 57
  • 128
  • Are you saying that a call to `setAutoFillBackground(true)` fails if the `background-color` property is set in the style? – G.M. Jul 13 '16 at 13:30
  • If I add a style for the background-color it automatically changes the value of the autofillbackground node to false. – SPlatten Jul 13 '16 at 13:32
  • Sorry but that's not what I'm asking. My question is... if I set the style to one that sets the `background-color` property, does a subsequent call to `setAutoFillBackground(true)` not set the `autoFillBackground` to true? That was the suggestion in my original comment. – G.M. Jul 13 '16 at 13:38
  • I've tried that and it doesn't work either, and when set to true it wouldn't use the colour set by the style which is what I was getting as as when specifying a colour in style it automatically disables the setting in XML. – SPlatten Jul 13 '16 at 13:39
0

Have you tried using QWidget::setWindowOpacity(1) where 1 = opaque and 0 = transperant.

Also try QGraphicsOpacityEffect.The following is code for setting the 50 percent opacity for a pushbutton but It can be extended to a widget as well.

ui->setupUi(this);
QGraphicsOpacityEffect * effect = new QGraphicsOpacityEffect(ui->pushButton);
effect->setOpacity(0.5); 
ui->pushButton->setGraphicsEffect(effect);
Sayantan Roy
  • 134
  • 1
  • 15