Results 1 to 4 of 4

Thread: How to draw image from shared memory in QGraphicsView?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2019
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default How to draw image from shared memory in QGraphicsView?

    I have an application that renders many hundreds large images as QGraphicsItems in a QGraphicsView. Currently this works ok, but I'm concerned about the memory required by Qt to store the internal representation of the QGraphicsItems. I need to hold one pointer to the image in memory myself so I can perform analyses and transformations on the image. And I understand that Qt stores a second copy of the image as a QPixmap with the QGraphicsItems, is that correct?

    How could I avoid this second copy? I'm willing to patch Qt or use a different rendering if that helps. I.e. I was wondering if I could instead draw OpenGL textures, and if these textures could share memory with a QImage? Did somebody try this, or does someone know how to render OpenGL textures that reference QImage memory (using shallow copy as described here: http://doc.qt.io/qt-5/implicit-sharing.html)?

    Thanks for hints!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to draw image from shared memory in QGraphicsView?

    Conversion from QImage to QPixmap might involve data duplication.

    Essentially it depends first on the how the plugin for the current platform adapter treats QPixmaps vs QImage.
    Secondly it depends on whether the format of the image is the different to the format required for the pixmap.

    In the ideal case a QPixmap shared the same internal buffer as the QImage it has been created from (Qt Raster paint engine based backends, image format equal).


    Having said that one thing that you could consider in any case is to create a QImage based GraphicsView item.
    I.e. creating your own equivalent to QGraphicsPixmapItem but either storing a QImage per item or referencing the image in your internal "image storage".

    Aside from giving you more control over image data copy and conversion, it would also allow you to make use of drawing time information such as "level of detail".

    Cheers,
    _

  3. #3
    Join Date
    Jan 2019
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to draw image from shared memory in QGraphicsView?

    Thanks a lot for your help! What you say makes perfect sense, but two questions:

    1. Do you know if there is a way to find out if QPixmap uses duplicate memory? Something like QPixmap::isShallow()?
    2. If I use QImage as the memory format, how do I draw it? QPainter only draws QImage by converting it to QPixmap internally, which is slower and still wastes the memory. Is there a direct drawing method that uses the QImage memory buffer?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to draw image from shared memory in QGraphicsView?

    Quote Originally Posted by emmenlau View Post
    Do you know if there is a way to find out if QPixmap uses duplicate memory? Something like QPixmap::isShallow()?
    Not easily, no.

    If you want to test for a single image just to check if your format/platform causes conversion, it might be possible to do QImage -> QPixmap -> QImage and compare the "bits()" pointer of the two images.

    In any case you might want to check your application with a memory profiler such as Heaptrack.
    E.g. measuring without creating pixmap items and then again with. Duplication of memory usage should be pretty obvious.

    Quote Originally Posted by emmenlau View Post
    If I use QImage as the memory format, how do I draw it? QPainter only draws QImage by converting it to QPixmap internally, which is slower and still wastes the memory.
    QPainter::drawImage() does not necessarily convert to QPixmap.

    There are many fast paths that result in memory-copy/bit-blit operations

    Cheers,
    _

Similar Threads

  1. shared memory - the right way to do it
    By fearu in forum Newbie
    Replies: 0
    Last Post: 27th October 2011, 00:46
  2. Does Qt support shared memory ?
    By Shawn in forum Qt Programming
    Replies: 11
    Last Post: 3rd November 2009, 18:07
  3. QT shared memory
    By sweetsubha2020 in forum Qt Programming
    Replies: 2
    Last Post: 18th January 2009, 06:30
  4. how to draw lines on image using QGraphicsView in Qt4
    By madhavilatha in forum What's New in Qt 4.4
    Replies: 1
    Last Post: 25th July 2008, 01:32
  5. How to clear shared memory?
    By THRESHE in forum Qt Programming
    Replies: 4
    Last Post: 22nd May 2008, 19:28

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.