【发布时间】:2023-03-22 01:10:02
【问题描述】:
我有一个小部件作为另一个小部件的叠加层。只要我不清除叠加层的背景,透明度就可以正常工作。
但我必须清除小部件以实现显示的“效果”。我试图解决20848594 中描述的初始问题(背景获得默认颜色),但除了将颜色更改为黑色之外,它没有任何效果......
有谁知道为什么应该透明的小部件不显示底层小部件?
代码如下:
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);
...
}
编辑: 刚刚注意到,在我的半透明绘画中使用 CompositionMode Source 而不是 SourceOver(在第一张图像中看到的渐变)也会导致它们成为红色到黑色的渐变而不是红色到透明的渐变。
这意味着除了WA_TranslucentBackground 或WA_NoSystemBackground 的初始透明度之外的所有透明度都不起作用。
【问题讨论】:
-
您是否尝试使用
Explicitly clear the old rectangle with a **transparent brush**而不是删除它(就像在回答您向我们展示的问题时所说的那样)? -
是的,我做到了,使用合成模式源 - 使用合成模式清除没有区别
标签: c++ qt qt5 transparency paint