将OSG嵌入QGraphicsView,QGraphicsCenter不显示

将OSG嵌入QGraphicsView,QGraphicsCenter不显示,qgraphicsview,openscenegraph,Qgraphicsview,Openscenegraph,出于某些原因,我们的软件的QWidget项被放入QGraphicscene中,并由QGraphicsView进行渲染。现在,我尝试将OpenScenegraph嵌入到QGraphicsView中,重新实现了QGraphicsView的trackground函数,如下所示 void OsgQGraphicsView::drawBackground(QPainter *painter, const QRectF &rect) { if(painter->paintEngine(

出于某些原因,我们的软件的QWidget项被放入QGraphicscene中,并由QGraphicsView进行渲染。现在,我尝试将OpenScenegraph嵌入到QGraphicsView中,重新实现了QGraphicsView的trackground函数,如下所示

void OsgQGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
{
    if(painter->paintEngine()->type() != QPaintEgin::OpenGL2)
    {
         // error manage
    }
    painter->save();
    painter->beginNativePainting();
    viewer_->frame();
    painter->endNativePainting();
    painter->restore();
}
    void OsgQGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
    {
       if(painter->paintEngine()->type() != QPaintEgin::OpenGL2)
       {
           // error manage
       }
       painter->save();
       painter->beginNativePainting();
       glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
       viewer_->frame();
       glPopClientAttrib();
       painter->endNativePainting();
       painter->restore();
   }

当osg的场景数据不为空时,Qgraphicscene中的项目将无法显示。

osg使用惰性状态更新台面,osg将不会在帧后重置opengl状态。看这个论坛 因此,我们可以在
viewer->frame()
之间推送和弹出opengl状态。但是解决方案是使用
glPushClientAttrib
而不是
glPushAttrib
,因为opengl使用客户机/服务器模式,客户机和服务器具有不同类型的状态请参见此论坛:。代码现在变成这样

void OsgQGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
{
    if(painter->paintEngine()->type() != QPaintEgin::OpenGL2)
    {
         // error manage
    }
    painter->save();
    painter->beginNativePainting();
    viewer_->frame();
    painter->endNativePainting();
    painter->restore();
}
    void OsgQGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
    {
       if(painter->paintEngine()->type() != QPaintEgin::OpenGL2)
       {
           // error manage
       }
       painter->save();
       painter->beginNativePainting();
       glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
       viewer_->frame();
       glPopClientAttrib();
       painter->endNativePainting();
       painter->restore();
   }
但这还不够,因为我们恢复清除qt的opengl状态以绘制图形项目,但在绘制qt项目后,osg的opengl状态变脏。因此,我们仍然需要重置osg的opengl状态,Thaks向Sean Spicer提供解决方案(它在带有qt5.6.3的osg3.4上工作。我尝试了osg3.6,它将崩溃):


这很有帮助,谢谢你!我们正在使用QQuickFramebufferObject。我们已经有了“最后的状态集”的东西,但仍然随机崩溃。现在,它可以通过额外的推/弹出调用稳定运行。