Important: Please read the Qt Code of Conduct -

Creating a 2D world in QT with bitmap

  • Hi guys,

    I'm searching for how to put two images above eachother and then make a part of the foreground image transparant so that it is like a hole and you see the second image? Because I want to use this for my worms game if you shoot in the map that a part of the map is gone like a hole and you see the background image?

    I already got this:

    @#include <QApplication>
    #include <QGraphicsEllipseItem>
    #include <QGraphicsScene>
    #include <QGraphicsView>

    int main( int argc, char **argv )
    QApplication app(argc, argv);

    QGraphicsScene scene;
    scene.setSceneRect( -100.0, -100.0, 500.0, 500.0 );

    QGraphicsView view( &scene );
    view.setRenderHints( QPainter::Antialiasing );
    QGraphicsPixmapItem item(QPixmap(":/images/test1.jpg"));
    QGraphicsPixmapItem item2(QPixmap(":/images/test2.jpg"));


    return app.exec();

    But I don't know how to make a part of it transparant? I tried with alpha and setmask but doens't work.

    Kind regards,

  • Hi!

    Consider displaying the image ( crater, demolished area or whatever you like), over background.
    It'll be much easier, and for processor I would say that also much faster.
    Also, if you wrap your code in @, it will syntax highlight it.


  • Oké, but then we still need to show only the thing that is destroyed so we still need to see what is destroyed? That isn't possible if we just paste it over the other image because then if there is something small there is always a big crater over the whole place...

    But thanks for your help already :-)

  • If you are having interactions with entities, than it isn't background any more.
    Have background completely separate ( for example sky).
    Land and other entities are then shown over background.
    Than, if you need to delete part of land, you only calculate are the effect, create new image, and copy that part of background onto the newly created image. Again, you show the image over land.
    You can also use polygons, for better collision detection etc.

    Or you could also resize the image of an crater and show it again :).

  • Yes I get that, but my only point is how can I remove something of the first image, I don't find functions to do that in Qgraphics

  • That's because, you can't just create a "hole" in the image.

    But, you can write your own function, which will change any necessary pixel at that image to transparent.

Log in to reply