如何绘制变暗的QPixmap?

How to draw darkened QPixmap?

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

您在 QPixmap 中没有像素访问权限,因此不可能遍历像素并使它们变暗。

然而,您可以使用透明的黑色画笔填充像素图,并使用多种合成模式进一步自定义结果。

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

一些较早的评论会在您的屏幕上留下一个深灰色的矩形。使用合成模式,会使所有可见的东西变暗,但任何透明区域仍然透明。

painter.setCompositionMode (QPainter::CompositionMode_DestinationIn);
painter.fillRect (rect, QBrush (QColor (0,0,0,128)));