How to achieve this effect with QPixmap?



  • Assume you have following QPixmap pixmap1 (also assume everything is a background except the black rectangle):

    pixmap1

    and pixmap2 (that has the same background as pixmap1 with the same black rectangle which is just moved a bit):

    pixmap2

    and I want to kind of merge them, so they'll look like following pixmap3:

    pixmap3

    Is there any way to achieve that effect in Qt?

    I thought I can use QPainter, but I don't know how to set the background's opacity to 0.

    Also, would it be possible to consider background as everything except the rectangle and the black circle, so the circle could also move and be merged like the rectangles simultaneously?

    E.g.:

    changed pixmap2 (as you can see, the circle also moved):

    changed pixmap2

    and changed pixmap3 (pixmap1 merged with changed pixmap2):

    changed pixmap3

    I apologise if something is unclear, let me know then.

    Thanks in advance.


  • Lifetime Qt Champion

    Hi and welcome to the forums
    Do you know location of the black rect?
    You could use clipping to not draw anything on top of pix1 that is not within
    area.
    If that is not an option it depends on what background is.
    If solid colors with no anti aliasing then something like
    http://doc.qt.io/qt-5/qimage.html#createMaskFromColor
    or QImage QImage::createHeuristicMask(bool clipTight = true)
    Might be able to mask out the "background" in a good way.

    I assume you try to visualize some movement of an object over a background you want to stay the same as in pix1 ?



  • @mrjj Hi, thanks for your fast reply and sorry for my late reply.

    The object moves almost all the time and I want to keep track of its movements. (it's not 100% true, because I need for some other reasons, but I think you get the idea).
    I've already thought about the mask, but what if the object would have a texture loaded from a file and it wouldn't be solid?
    The idea with clipping would be good, though what if the are couple of objects? It wouldn't be too efficient, IMO.

    I think either I've explained something wrong or there's no good solution for me. The first option is more likely.


  • Qt Champions 2018

    Do you need to do it with QPixmap or are you just trying stuff? this should be something very easy to do with QtQuick Scenegraph/QGraphicsView



  • @VRonin I grab this pixmap from QGraphicsView, so could you post your solution, please?


  • Qt Champions 2018

    Are the items all different graphic items?



  • @VRonin Yes, they are, but the pixmap is just grabbed QGraphicsView's content using grab() method.