Clear Transparent Background for QWidget

4.8k views Asked by At

I have a widget as on overlay for another widget. The transparency works fine, as long as I don't clear the background of the overlay.

Overlay still transparent

But I have to clear the widget to acutalize the displayed "effects". I tried to solve the inital problem (the background getting the default color) like described in 20848594, but except changing the color to black it had no effect...

After "Clearing"

Has anyone an idea why the widget which should be transparent does not display the underlaying widget(s)?


Here the code:

SudokuMarkerOverlayWidget::SudokuMarkerOverlayWidget(QWidget* parent, uint const fieldSize) : QWidget(parent)
{
    // Translucent should be the correct one
    setAttribute(Qt::WA_TranslucentBackground);
    //setAttribute(Qt::WA_NoSystemBackground);
    setAttribute(Qt::WA_TransparentForMouseEvents);

    ...
}

void SudokuMarkerOverlayWidget::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    painter.setRenderHint( QPainter::Antialiasing );

    // Tried this too, no difference
    // painter.setCompositionMode(QPainter::CompositionMode_Source);
    // painter.fillRect( this->rect(), Qt::transparent );
    painter.setCompositionMode(QPainter::CompositionMode_Clear);
    painter.eraseRect( this->rect() );
    painter.setCompositionMode(QPainter::CompositionMode_SourceOver);

    ...
}

EDIT: Just noticed, using CompositionMode Source instead of SourceOver for my semi-transparent painting (gradient seen in first image) also causes them to be a red-to-black-gradient instead of red-to-transparent.

That means every transparency except the inital by WA_TranslucentBackground or WA_NoSystemBackground isn't working.

1

There are 1 answers

3
Jay On

Widgets in Qt can be either a 'native' or 'alien' type. In one case they're a separate operating system window. In some operating systems a transparent window isn't supported.

You might want to consider using QML if you're after fancy visual effects. That's what it was designed for.