如何绘制变暗的 QPixmap?

Gri*_*ort 3 c++ qt qpixmap qpainter qt5

我正在寻找一种快速有效的方法来使用 QPainter 绘制 QPixmap,但让像素图看起来比正常情况更暗。是否有某种过滤器或效果可以在绘制时应用于 QPixmap 或 QPainter 以创建这种效果?

dte*_*ech 6

You don't have pixel access in QPixmap, so going over the pixels and darkening them is out of the question.

You can however fill a pixmap with a transparent black brush, and use a number of composition modes to further customize the result.

    QPainter painter(this);
    QPixmap pm("d:/test.jpg");
    painter.drawPixmap(QRect(0, 0, 400, 200), pm);
    painter.translate(0, 200);
    painter.drawPixmap(QRect(0, 0, 400, 200), pm);
    painter.fillRect(QRect(0, 0, 400, 200), QBrush(QColor(0, 0, 0, 200)));
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明