View Full Version : Overlaying two QImages in a QGraphicsview

21st October 2010, 13:02

I have two QImages, one is a QImage::Format_Indexed8 and the other is loaded fropm a pgm file.

I want to overlay the images so that the Indexed_8 shows through the pgm image, i.e the pgm is transparent. Both images are the same size.

I am using a QGraphicsView to display the images.

How do I do this

Best Regards

21st October 2010, 13:55
What have you tried so far, and what didn't work for you?

21st October 2010, 14:34

I create the image from a memory buffer and set the colour table for the image, then load the mask file and add them to the QGraphicsScene , then update the viewport as shown below

//initialise buffer, width and height
image = new QImage(buffer,width, height, QImage::Format_Indexed8);
setMaximumSize(1024, 1024);
setMinimumSize(128, 128);
mask = new QImage("mask_1.pgm","PGM");

//update the scene every second using a timer

viewport()->update(); //! force repaint

Thanks for your help

21st October 2010, 16:20
Why don't you just draw both QImages on one pixmap?

21st October 2010, 19:45

I dont quite understand.

Do you mean use Qpainter::drawImage and just draw them at the same location in the QGraphicsView?.

How is this different from the code above (which does not give the results I'm after)


22nd October 2010, 09:29
If I understand correctly, you want to overlay two images one on top of the other, where both of them have the same dimensions.
So if you render the first on to a pixmap, and then the other on to the same pixmap the end result should be exactly what you want.
You then only add that pixmap to the view.
This way you only use one pixmap.

To make your code work, I think what you need to do is set the alpha channel to the pixmaps - more specifically the second one that comes on top, and also use QGraphicsPixmapItem::setShapeMode ( ShapeMode mode )
to set 'mask' mode.