Results 1 to 6 of 6

Thread: Help:Export QGraphicsView to Image File

  1. #1
    Join Date
    Dec 2007
    Posts
    4
    Thanks
    3

    Default Help:Export QGraphicsView to Image File

    Hi, All.

    I'm trying to export the GraphicsScene's content to Image File.
    And I use the code example from the documentation.

    Qt Code:
    1. scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));
    2.  
    3. QPixmap pixmap;
    4. QPainter painter(&pixmap);
    5. painter.setRenderHint(QPainter::Antialiasing);
    6. scene.render(&painter);
    7. painter.end();
    8.  
    9. pixmap.save("scene.png");
    To copy to clipboard, switch view to plain text mode 

    However, it failed.

    The error message show that:
    QPainter::begin:Cannot paint on a null pixmap
    QPainter::setRenderHint: Painter must be active to set rendering hints
    ....

    Anyone know what's wrong with the example code(I already correct some error in the given documentation)

    I use Qt 4.3.2 on Linux platform
    Last edited by marcel; 26th December 2007 at 22:22. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help:Export QGraphicsView to Image File

    That's because, as the warning says, you're painting on a null pixmap. You first need to initialize the pixmap to the scene's size.
    See QGraphicsScene::sceneRect()

  3. The following user says thank you to marcel for this useful post:

    cometyang (26th December 2007)

  4. #3
    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: Help:Export QGraphicsView to Image File

    The size doesn't have to be the size of the scene. This is perfectly correct:
    Qt Code:
    1. //...
    2. QImage img(1024,768,QImage::Format_ARGB32_Premultiplied);
    3. QPainter p(&img);
    4. scene.render(&p);
    5. p.end();
    6. img.save("XXX.png");
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to wysota for this useful post:

    cometyang (26th December 2007)

  6. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help:Export QGraphicsView to Image File

    Quote Originally Posted by wysota View Post
    The size doesn't have to be the size of the scene. This is perfectly correct:
    Qt Code:
    1. //...
    2. QImage img(1024,768,QImage::Format_ARGB32_Premultiplied);
    3. QPainter p(&img);
    4. scene.render(&p);
    5. p.end();
    6. img.save("XXX.png");
    To copy to clipboard, switch view to plain text mode 
    I assumed he wanted to export the entire scene. Of course, the output image can be initialized to any size, as long as it is not (0,0).

  7. The following user says thank you to marcel for this useful post:

    cometyang (26th December 2007)

  8. #5
    Join Date
    Dec 2007
    Posts
    4
    Thanks
    3

    Default Re: Help:Export QGraphicsView to Image File

    Thanks for your help.

    The problem solved!

  9. #6
    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: Help:Export QGraphicsView to Image File

    Quote Originally Posted by marcel View Post
    I assumed he wanted to export the entire scene.
    If you don't pass the size to QGraphicsScene::render(), it will always draw the entire scene. The painter (image) size is only to determine the scale which is to be used to draw the scene.

Similar Threads

  1. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  2. Finding marks on scanned image for alignment
    By caduel in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2007, 02:10
  3. Can't Write Image as Jpeg file
    By noufalk in forum Qt Programming
    Replies: 4
    Last Post: 25th July 2007, 14:53
  4. no image displayed on QGraphicsView
    By sincnarf in forum Qt Programming
    Replies: 9
    Last Post: 28th June 2007, 12:33
  5. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21

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.