SPlatten
SPlatten

Reputation: 5762

QWidget background transparent

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.

Upvotes: 1

Views: 2680

Answers (4)

Martin Ellguth
Martin Ellguth

Reputation: 61

Even though this question is very old, I still stumbled upon this thread while having the same problem and I was out of luck with the "setAutoFillBackground(true)" approach. Sometimes, you just want a distinct, opaque background for diagnosing sizing and placement issues of a QWidget. What worked best for me, was to override virtual void paintEvent(QPaintEvent*); and implement it as follows:

void MyWidget::paintEvent(QPaintEvent* e)
{
  QPainter p(this);
  p.fillRect(rect(), palette().background());
  QWidget::paintEvent(e);
}

Replace palette().background() by e.g. QBrush(QColor(255, 255, 0)) for a yellow background.

Upvotes: 0

Sayantan Roy
Sayantan Roy

Reputation: 134

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);

Upvotes: 0

SPlatten
SPlatten

Reputation: 5762

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.

Upvotes: 2

jonspaceharper
jonspaceharper

Reputation: 4367

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(...);

Upvotes: 1

Related Questions