Results 1 to 9 of 9

Thread: Clever image zooming. What to write in PaintEvent?

  1. #1
    Join Date
    Nov 2007
    Location
    Russia, Moscow
    Posts
    21
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Clever image zooming. What to write in PaintEvent?

    Hello. I'm making a simple image (geographic map) editor for myself.
    1) I have a QScrollArea and a custom Widget inserted into the scroll area.
    2) I have a large image. 5000x5000 pixels. I read it into a QPixmap.
    3) I have a second qpixmap. i put a scaled copy of my image into that pixmap.
    in the paintevent of the widget i draw the second pixmap.

    The problem is a huge memory usage. For example if i whant a 200% zoom i make a doubled copy of my image and store it in memory together with the original image..

    Maybe there's a way to write a more efficient PaintEvent?
    Thx.
    Last edited by ModeZt; 12th December 2007 at 23:37.

  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: Clever image zooming. What to write in PaintEvent?

    I think that a better way would be to use the Graphics View framework.
    It is optimized in many ways, to handle many, and large items.

    Hopefully Andreas (the troll who developed it) will see this thread, and might comment with more details.
    ==========================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
    Nov 2007
    Location
    Russia, Moscow
    Posts
    21
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Clever image zooming. What to write in PaintEvent?

    Ok. I'm now looking at the QGraphicsScene/View.
    I made a
    Qt Code:
    1. myGraphicsScene->setBackgroundBrush( QBrush( QImage("myimage.bmp") ) );
    To copy to clipboard, switch view to plain text mode 
    It works. But when i zoomIn/Out whith myGraphicsView->scale(..) the image resizeing is not smooth.. I mean there's no bilinear filtering and the image looks bad when zoomed in/out.

  4. #4
    Join Date
    Nov 2007
    Location
    Russia, Moscow
    Posts
    21
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Clever image zooming. What to write in PaintEvent?

    And i got the same problem with unsmoothed image when i
    Qt Code:
    1. myGraphicsScene->addPixmap( QPixmap::fromImage(image) );
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Clever image zooming. What to write in PaintEvent?

    try
    Qt Code:
    1. graphicsView->setRenderHint (QPainter::SmoothPixmapTransform);
    To copy to clipboard, switch view to plain text mode 
    and/or
    Qt Code:
    1. QGraphicsPixmapItem* item = myGraphicsScene->addPixmap( QPixmap::fromImage(image) );
    2. item->setTransformationMode(Qt::SmoothTransformation);
    To copy to clipboard, switch view to plain text mode 
    and/or
    Qt Code:
    1. graphicsView->setViewport(new QGLWidget)
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to spud for this useful post:

    ModeZt (15th December 2007)

  7. #6
    Join Date
    Nov 2007
    Location
    Russia, Moscow
    Posts
    21
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Clever image zooming. What to write in PaintEvent?

    many thanx, spud!
    this solution works!
    Qt Code:
    1. QGraphicsPixmapItem* item = addPixmap( QPixmap::fromImage(image) );
    2. item->setTransformationMode(Qt::SmoothTransformation);
    To copy to clipboard, switch view to plain text mode 
    ( BTW it's not working together with OpenGL viewport )

    as mentioned in the http://doc.trolltech.com/4.3/qgraphicspixmapitem.html the result is not as good as QPixmap::scale(), but the performanse boost is huge!

  8. #7
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Clever image zooming. What to write in PaintEvent?

    Glad it works!
    Just out of curiosity, how does setting the OpenGL viewport fail? In your case OpenGL should give you another huge performance boost, especially on linux systems(because of the way X handles server side pixmaps).

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Clever image zooming. What to write in PaintEvent?

    Quote Originally Posted by spud View Post
    Just out of curiosity, how does setting the OpenGL viewport fail? In your case OpenGL should give you another huge performance boost, especially on linux systems(because of the way X handles server side pixmaps).
    It is probably not smooth. That might need a GLWidget with sample buffers turned on.

  10. #9
    Join Date
    Nov 2007
    Location
    Russia, Moscow
    Posts
    21
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Clever image zooming. What to write in PaintEvent?

    Quote Originally Posted by wysota View Post
    It is probably not smooth. That might need a GLWidget with sample buffers turned on.
    on both Linux and Win i don't get smoothing if i change the viewport.
    Qt Code:
    1. graphicsView->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    To copy to clipboard, switch view to plain text mode 

    but that's not all the troubles i have with OpenGL viewport.
    with small images it works nicely, but with the 80 megabytes 5000x5000 bmp it does not.
    after adding this pixmap to my graphicsScene i don't see any image at all. the scrolling becomes very laggy and zooming in/out takes lots of time.

    at the moment i'm quite happy with the simple QWidget viewport.
    many thanks for usefull advice.

Similar Threads

  1. Replies: 3
    Last Post: 11th March 2007, 13:04

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.