【问题标题】:Render QWidget in QPixmap with system background使用系统背景在 QPixmap 中渲染 QWidget
【发布时间】:2021-11-28 14:12:01
【问题描述】:

当我使用带有透明对象的 QOpenGLWidget 子级渲染 QWidget 时,当我尝试截取如下图所示的屏幕截图时,我观察到了日晒效果:

问题来自系统后台,因为当我删除它时,问题就消失了:

.

我的问题是:用系统背景在 QPixmap 上渲染 QWidget 的解决方案是什么?

这是我的渲染代码:

    QWidget widget;
    QPixmap pixmap(widget.size());
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::Antialiasing);
    widget.render(&painter, QPoint(), QRegion(), QWidget::DrawWindowBackground /* the problem is this render flag */ | QWidget::IgnoreMask | QWidget::DrawChildren);
    pixmap.save("screenshot.png");

setAutoFillBackground(true); 属性和 grab(); 方法与第一个屏幕截图一样。

编辑:没有 OpenGL 问题。当我混合两个像素图(第一个带有背景,第二个带有小部件子图)时,日晒问题仍然存在。

【问题讨论】:

    标签: c++ qt opengl qwidget qpixmap


    【解决方案1】:

    解决方案是在我的 QOpenGLWidget 中将具有 Window 角色的 QPalette 设置为黑色。

    QPalette currentWidgetPalette{ palette() };
    currentWidgetPalette.setColor(QPalette::Window, Qt::black);
    setPalette(currentWidgetPalette);
    

    编辑:我找到了一种解决方案,可以用我的小部件以透明方式绘制目标并使用我的小部件正确组合像素图。

    painter.setCompositionMode(QPainter::CompositionMode_Destination);
    painter.fillRect(result.rect(), Qt::transparent);
    painter.setCompositionMode(QPainter::CompositionMode_Source);
    widget.render(&painter, QPoint(), QRegion(), QWidget::DrawWindowBackground | QWidget::IgnoreMask | QWidget::DrawChildren);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 2018-02-04
      • 1970-01-01
      相关资源
      最近更新 更多