Results 1 to 6 of 6

Thread: Overlaying two QImages in a QGraphicsview

  1. #1
    Join Date
    May 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Overlaying two QImages in a QGraphicsview

    Hi

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Overlaying two QImages in a QGraphicsview

    What have you tried so far, and what didn't work for you?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    May 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Overlaying two QImages in a QGraphicsview

    Hi

    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

    Qt Code:
    1. //initialise buffer, width and height
    2. .
    3. .
    4. image = new QImage(buffer,width, height, QImage::Format_Indexed8);
    5. image->setColorTable(cmap);
    6. setMaximumSize(1024, 1024);
    7. setMinimumSize(128, 128);
    8. mask = new QImage("mask_1.pgm","PGM");
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //update the scene every second using a timer
    2.  
    3. scene->addPixmap(QPixmap::fromImage(image));
    4. scene->addPixmap(QPixmap::fromImage(mask));
    5. viewport()->update(); //! force repaint
    To copy to clipboard, switch view to plain text mode 


    Thanks for your help

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Overlaying two QImages in a QGraphicsview

    Why don't you just draw both QImages on one pixmap?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    May 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Overlaying two QImages in a QGraphicsview

    Hi

    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)


    Thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Overlaying two QImages in a QGraphicsview

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Overlaying Widgets on top of VideoWidget
    By opsimath in forum Qt Programming
    Replies: 2
    Last Post: 9th June 2011, 14:29
  2. QGraphicsView
    By Maluko_Da_Tola in forum Newbie
    Replies: 1
    Last Post: 24th July 2010, 06:40
  3. Replies: 0
    Last Post: 25th May 2009, 11:00
  4. help with QGraphicsView
    By Erlendhg in forum Qt Programming
    Replies: 6
    Last Post: 22nd April 2007, 21:26
  5. Question about QImages
    By SkripT in forum Qt Programming
    Replies: 14
    Last Post: 27th January 2006, 12:45

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.