Make QT Widgets semi-opaque

Ale picture Ale · Sep 15, 2011 · Viewed 7.8k times · Source

I am working with a QWidget elements which contains child elements, what I need is some way to make this widget semi transparent, completely, including its childs.

I have seen a method for QWidgets which is QWidget::setWindowOpacity() but this works only if the widget is a window itself, and in my case this widget is part of a layout.

The goal of all this, is me being able to make this widget fade when appearing or disappearing.

Thanks for any ideas, hopefully not making a custom widget, but if there is no more alternatives, I can do it anyway.

Answer

lemoran picture lemoran · Sep 15, 2011

You can use QGraphicsOpacityEffect.

A sample code fragment for 50% transparency would be:

ui->setupUi(this);
QGraphicsOpacityEffect * effect = new QGraphicsOpacityEffect(ui->pushButton);
effect->setOpacity(0.5);
ui->pushButton->setGraphicsEffect(effect);