【问题标题】:Clear Transparent Background for QWidget为 QWidget 清除透明背景
【发布时间】: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_TranslucentBackgroundWA_NoSystemBackground 的初始透明度之外的所有透明度都不起作用。

【问题讨论】:

  • 您是否尝试使用Explicitly clear the old rectangle with a **transparent brush** 而不是删除它(就像在回答您向我们展示的问题时所说的那样)?
  • 是的,我做到了,使用合成模式源 - 使用合成模式清除没有区别

标签: c++ qt qt5 transparency paint


【解决方案1】:

Qt 中的小部件可以是'native' or 'alien' type。在一种情况下,它们是一个单独的操作系统窗口。在某些操作系统中,不支持透明窗口。

如果您追求精美的视觉效果,您可能需要考虑使用 QML。这就是它的设计目的。

【讨论】:

  • 好的,你可能有一点,但仍然 - 有工作的透明度,只是清理不起作用
  • 我最后的评论似乎比我的意思更糟糕,当然,如果我不花太多钱就不能让它工作,我会考虑使用替代品
  • 这可能就像在构造函数上指定一个窗口标志一样简单。我花了很多时间在 Windows 上制作透明度,但它不是很健壮。不用担心。
猜你喜欢
  • 1970-01-01
  • 2012-07-26
  • 1970-01-01
  • 2011-06-17
  • 2013-06-27
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
  • 2016-08-18
相关资源
最近更新 更多