我有一个小部件作为另一个小部件的覆盖。透明度工作正常,只要我不清除覆盖的背景。清除QWidget的透明背景
但我要明确的小部件acutalize所显示的“效果”。我试图解决像20848594描述的inital问题(背景获取默认的颜色),但只是改变颜色为黑色,它没有效果...
有没有人一个想法,为什么小部件哪个应该是透明的不显示下层的小部件?
这里的代码:
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源代替SourceOver用于我的半透明画(梯度在第一图像可见)也使得它们是红色黑到渐变而不是从红到透明。
这意味着除了inital以外的所有透明度通过WA_TranslucentBackground
或WA_NoSystemBackground
不起作用。
你尝试过'用**透明画笔**清楚地清除旧的矩形而不是擦除它(就像它在你给我们展示的问题的答案中所说的那样)? – dazewell
是的,我做了,与组成模式源 - 没有区别使用组合模式清除 –