Results 1 to 3 of 3

Thread: [QWidget] Save content to image on paintEvent

  1. #1

    Default [QWidget] Save content to image on paintEvent

    Hey,

    I'm visualizing some simulation results in a QWidget in a GUI app, and inside the QWidget I paint using QPainter. Now I want to save the simulation progress in a video file, so I wanted to start by saving a bitmap of each timestep.

    My simple idea was:
    - In my redraw function (called from the paintEvent), render the QWidget to a QPixmap and save that:
    Qt Code:
    1. QPixmap pixmap(this->size());
    2.  
    3. this->render(&pixmap, QPoint(), QRegion(this->frameGeometry()));
    4. pixmap.save("example.png");
    To copy to clipboard, switch view to plain text mode 

    But since render() seems to call the paintEvent again, and that painEvent calls the whole image capturing routine, I always get stuck in an infinite, recursive paintEvent call.

    So my other idea was to save the bitmap from the thread that's actually outputting the simulation results and rendering them in my QWidget:
    Qt Code:
    1. canvas->setGrid(*grid);
    2. canvas->update();
    3.  
    4. canvas->saveFrame();
    To copy to clipboard, switch view to plain text mode 
    saveFrame contains the same code as above, but this doesn't work because "it is not safe to use pixmaps outside the GUI thread".

    So what can I do here? Any way to get the current widget pixmap without re-rendering?? That would be perfect because rendering may be complex and take some time, so I want to capture the currently displayed content right away, without re-rendering.

    Any ideas or help? Thanks!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: [QWidget] Save content to image on paintEvent

    Seems like this should work. In your paintEvent():
    • render the widget content to an off-screen QPixmap using a QPainter on the pixmap.
    • drawPixmap() onto the screen using QPainter on "this"
    • call QPixmap::save() which does not cause a redraw.

    What effect the file operations will have on performance is entirely another issue.

  3. #3

    Default Re: [QWidget] Save content to image on paintEvent

    Nice, that worked perfectly! So far I don't see the performance drop too much, so I think I gonna use that method.

    Thanks!

Similar Threads

  1. Replies: 4
    Last Post: 29th May 2012, 23:37
  2. paintEvent and Pixmap - not deleting old content!
    By StarShaper in forum Qt Programming
    Replies: 1
    Last Post: 17th February 2012, 09:59
  3. Replies: 6
    Last Post: 20th May 2011, 17:56
  4. read ini file content and save into array
    By cooper in forum Newbie
    Replies: 8
    Last Post: 14th March 2011, 21:33
  5. Save content of widget to pixmap
    By vql in forum Qt Programming
    Replies: 2
    Last Post: 4th April 2008, 22:26

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.