Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsView render() execute failed when set QOpenglWidget as viewport
Qt 6.11 is out! See what's new in the release blog

QGraphicsView render() execute failed when set QOpenglWidget as viewport

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 532 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    luzhigang
    wrote on last edited by luzhigang
    #1

    I want to use QOpenglwidget combie with QGraphicsView FrameWork.So I set the QOpenglWidget as the QgraphicsView's viewport.The function of QOpenglWidget is draw background texture.
    Then I add a QGraphicsTextItem to QGraphicsScene. But When I use QGraphicsView's render() function to print the screen contents into a QImage.I found that only QGrahicsTextItem is saved into QImage, the texture is not saved.
    But when I directly draw texture use QPainter in QGraphicsView's drawbackground() instead of use QOpenglwidget, both the QGraphicsTextItem and background texture are save into qimage.
    Why?
    Below is the render related source code.

    	m_pMyOpenglWidget = new MyOpenGLWidget(this);
    	setViewport(m_pMyOpenglWidget);
    	setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    
    QImage image(1920, 960, QImage::Format_ARGB32_Premultiplied);
    QPainter painter(&image);
    ui.graphicsView->render(&painter, QRectF(0, 0, showWidth, showHeight), QRect(renderLeft, renderTop, showWidth, showHeight));
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    image.save("D://test.png");
    
    void MyGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
    {
            //Method 1:direct draw use Painter.
    	//painter->drawImage(QPoint(0, 0), QImage("D:\\backup\\picture\\Panorama2D_1920_960.jpg"));
    
      //method2:OpenglWidget Method
      m_pMyOpenglWidget->drawPicturepaintGL();
    }
    
    

    Below is Render()'s result
    0_1522145252190_test.png

    below is real window contents
    0_1522145273579_无标题.jpg

    K 1 Reply Last reply
    0
    • L luzhigang

      I want to use QOpenglwidget combie with QGraphicsView FrameWork.So I set the QOpenglWidget as the QgraphicsView's viewport.The function of QOpenglWidget is draw background texture.
      Then I add a QGraphicsTextItem to QGraphicsScene. But When I use QGraphicsView's render() function to print the screen contents into a QImage.I found that only QGrahicsTextItem is saved into QImage, the texture is not saved.
      But when I directly draw texture use QPainter in QGraphicsView's drawbackground() instead of use QOpenglwidget, both the QGraphicsTextItem and background texture are save into qimage.
      Why?
      Below is the render related source code.

      	m_pMyOpenglWidget = new MyOpenGLWidget(this);
      	setViewport(m_pMyOpenglWidget);
      	setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
      
      QImage image(1920, 960, QImage::Format_ARGB32_Premultiplied);
      QPainter painter(&image);
      ui.graphicsView->render(&painter, QRectF(0, 0, showWidth, showHeight), QRect(renderLeft, renderTop, showWidth, showHeight));
      painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
      image.save("D://test.png");
      
      void MyGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
      {
              //Method 1:direct draw use Painter.
      	//painter->drawImage(QPoint(0, 0), QImage("D:\\backup\\picture\\Panorama2D_1920_960.jpg"));
      
        //method2:OpenglWidget Method
        m_pMyOpenglWidget->drawPicturepaintGL();
      }
      
      

      Below is Render()'s result
      0_1522145252190_test.png

      below is real window contents
      0_1522145273579_无标题.jpg

      K Offline
      K Offline
      kenchan
      wrote on last edited by
      #2

      @luzhigang some code showing what you did would help people figure out what is wrong.
      preferably minimal app so people can try to reproduce it...

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved