3

I'm looking for a fast and effective way to draw a QPixmap with QPainter, but have the pixmap appear darker then normal. Is there some sort of filter or effect that can be applied to either the QPixmap or QPainter while drawing to create this effect?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Griffort
  • 1,174
  • 1
  • 10
  • 26
  • 1
    You could generate a second pixmap in code that has a black or gray color and is exactly the same size as the pixmap you want to draw and then just play with the alpha value of the this dark pixmap to act as a "filter". Make sure this secondary pixmap is draw on top obviously. – Developer Paul Mar 22 '18 at 18:17

2 Answers2

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

enter image description here

dtech
  • 47,916
  • 17
  • 112
  • 190
  • With the raster backend, `QPixmap` is a very thin wrapper around `QImage`. Pixmap-image conversion is essentially a no-op :) – Kuba hasn't forgotten Monica Mar 22 '18 at 20:09
  • @KubaOber I am not sure this is true, with the raster backend QImage is tangibly slower than QPixmap. I've done testing in windows and linux. IIRC the pixmap uses platform specific implementations whereas the image is Qt's own generic implementation. – dtech Mar 22 '18 at 20:11
  • This hasn't been true for close to a decade. Pixmap doesn't use anything platform specific at all. QImage is only slower than the pixmap when it has a format that's less efficient to paint on. – Kuba hasn't forgotten Monica Mar 22 '18 at 20:23
  • It is possible, I tested before Qt 5 came out, which is close to a decade. About 7 years probably. – dtech Mar 22 '18 at 20:36
  • Ack, unfortunately, I need to darken a QPixmap that isn't perfectly rectangular. It's a unique shape. How can I darken that part specifically? – Griffort Mar 23 '18 at 01:29
  • 1
    @Griffort There is no such thing as a non-rectangular pixmap. What you mean is its visual representation is not rectangular. In which case it depends on how is that representation defined - if it is an alpha channel you can use a custom composition mode. Otherwise, you will have to use a pre-defined stencil, that's essentially a black image with an alpha channel that cuts it out to the desired shape. – dtech Mar 23 '18 at 09:11
2

Some of the earlier comments will leave a dark gray rectangle on your screen. Using the composition mode, will darken everything visible, yet leaving any transparent areas still transparent.

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