Results 1 to 3 of 3

Thread: QGraphicsView + native opengl painting -> rendering to image

  1. #1
    Join Date
    Jan 2011
    Location
    Netherlands
    Posts
    17
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QGraphicsView + native opengl painting -> rendering to image

    I have a QGraphicsView and on this view I place several subclassed QGraphicsItems which implement their own paint method. Now I want to render the graphicsview/scene to an image and save this to disk. I use the following method to render the scene to an image:

    Qt Code:
    1. QPainter *pngPainter = new QPainter();
    2. QImage *image = new QImage(QSize(width,height), QImage::Format_ARGB32_Premultiplied);
    3.  
    4. pngPainter->begin(image);
    5. pngPainter->setRenderHint(QPainter::Antialiasing);
    6. _ui->graphicsView->scene()->render(pngPainter);
    7. pngPainter->end();
    8. image->save(fileName);
    9.  
    10. delete pngPainter;
    11. delete image;
    To copy to clipboard, switch view to plain text mode 

    This works perfectly when I use QPainter commands in the paint procedure of the QGraphicsItems, like so (block is a QRectF):

    Qt Code:
    1. void CNodeGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. // color setup
    4. painter->setPen(Qt::red);
    5. painter->setBrush(Qt::red);
    6.  
    7. painter->drawRect(block);
    8. }
    To copy to clipboard, switch view to plain text mode 

    However, If (for performance reasons and in order to use shaders) I use native opengl commands, like so:

    Qt Code:
    1. void CNodeGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. painter->beginNativePainting();
    4. glBegin(GL_QUADS);
    5. setGLColor(Qt::red); // Own function to set glColor3f;
    6. glTexCoord2f (0.0, 0.0);glVertex2d(block.left(),block.top());
    7. glTexCoord2f (1.0, 0.0);glVertex2d(block.right(),block.top());
    8. glTexCoord2f (1.0, 1.0);glVertex2d(block.right(),block.bottom());
    9. glTexCoord2f (0.0, 1.0);glVertex2d(block.left(),block.bottom());
    10. glEnd();
    11. painter->endNativePainting();
    12. }
    To copy to clipboard, switch view to plain text mode 

    It no longer works and the output is an entire black image. The rendering on the QGraphicsView gives the expected result though (a red rectangle). Any ideas on how to solve this / make it work with native opengl commands?

    I know I can grab the framebuffer:
    Qt Code:
    1. QImage img = ((QGLWidget*)_ui->graphicsView->viewport())->grabFrameBuffer(true);
    2. img.save(fileName, "PNG", 100);
    To copy to clipboard, switch view to plain text mode 

    This works, however the output image is only the size of the graphicsView, I want to be able
    to output high-res images.

  2. #2
    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: QGraphicsView + native opengl painting -> rendering to image

    Try rendering to a gl pixel (or frame) buffer and then converting the result to a QImage. By the way, never create QImage instances on the heap, it makes no sense.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Stef (8th October 2011)

  4. #3
    Join Date
    Jan 2011
    Location
    Netherlands
    Posts
    17
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: QGraphicsView + native opengl painting -> rendering to image

    Thank you for pointing me into the right direction. I now render to an offscreen framebuffer and convert that to QImage. For completeness here is my solution:

    Qt Code:
    1. QGLFramebufferObjectFormat format;
    2. format.setSamples(16);
    3.  
    4. QGLFramebufferObject renderFbo(4096, 4096);
    5.  
    6. QPainter fboPainter(&renderFbo);
    7.  
    8. fboPainter.setRenderHint(QPainter::Antialiasing);
    9. fboPainter.setRenderHint(QPainter::HighQualityAntialiasing);
    10. _ui->graphicsView->scene()->render(&fboPainter);
    11. fboPainter.end();
    12.  
    13. renderFbo.toImage().save(fileName, "PNG", 100);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Image rendering and zooming (QGraphicsView)
    By Sergex in forum Qt Programming
    Replies: 7
    Last Post: 6th October 2011, 12:51
  2. Painting on top of real-time rendering windows
    By haji in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2011, 21:02
  3. Replies: 4
    Last Post: 20th October 2010, 08:13
  4. QGraphicsView + OpenGL -> Pixmap rendering issue
    By JoergRe in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2008, 18:41
  5. problem in rendering opengl on QGraphicsView
    By Sandip in forum Qt Programming
    Replies: 17
    Last Post: 15th April 2008, 08:27

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.