简体   繁体   English

清除QWidget的透明背景

[英]Clear Transparent Background for QWidget

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. 只要我不清除叠加层的背景,透明度就可以正常工作。

覆盖物仍然透明

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... 我试图解决初始问题(背景获得默认颜色),如20848594中所述 ,但除了将颜色更改为黑色外,它没有任何作用...

在“清除”之后

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. 编辑:刚注意到,对于我的半透明绘画(在第一张图像中看到的渐变),使用CompositionMode Source而不是SourceOver也会使它们成为红色到黑色的渐变,而不是红色到透明。

That means every transparency except the inital by WA_TranslucentBackground or WA_NoSystemBackground isn't working. 这意味着除WA_TranslucentBackgroundWA_NoSystemBackground之外的所有透明度均不起作用。

Widgets in Qt can be either a 'native' or 'alien' type . Qt中的小部件可以是'native'或'alien'类型 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. 如果您追求精美的视觉效果,则可能要考虑使用QML。 That's what it was designed for. 这就是它的设计目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM