简体   繁体   中英

Render QWidget in QPixmap with system background

When I render a QWidget with a QOpenGLWidget child with transparency objects, I observe a solarization effect when I tried to take a screenshot like the following picture:

曝光截图

The problem comes with the system background because when I remove it, the problem disappear:

没有曝光但没有背景的截图 .

My question is: What is the solutions to render a QWidget on a QPixmap with the system background?

Here my render code:

    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");

The property setAutoFillBackground(true); and the method grab(); do the same like the first screenshot.

EDIT: There is no OpenGL problem. When I mix two pixmaps (the first with the background and the second with widget children), the solarization problem is still present.

A solution is to set a QPalette with Window role to black in my QOpenGLWidget.

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

EDIT: I found a solution to paint destination in tansparent and source with my widget to compose the pixmap correctly.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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