Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Set QGraphicsScene dynamically in QGraphicsView with OpenGL Viewport



  • Hi all

    I have a problem, i'm developing a graphical program under Windows, there are few QGraphicsScene and one QGraphicsView that it is possible to change the Scenes in runtime with a lot of graphics items, the problem is when I use Qwidget viewport everything works but when I switch to OpenGL viewport when I change the scene the content of previous scene still appear on the QGraphicsView and the contents of new Scene appear too.

    what is the problem ? is it the changing Scenes method best solution or should I change the method ?

    here is the code to setup View

    m_viewPort     (new QOpenGLWidget (this))
    .......
    
      QSurfaceFormat format;
        format.setProfile(QSurfaceFormat::CoreProfile);
        format.setDepthBufferSize(24);
        format.setStencilBufferSize(8);
        format.setSamples(4);
    
        m_viewPort->setFormat(format);
    
        ui->gV->setViewport(m_viewPort);
    
        ui->gV->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    
        //ui->gV->setCacheMode(QGraphicsView::CacheBackground);
        ui->gV->setRenderHints(QPainter::Antialiasing| QPainter::HighQualityAntialiasing | QPainter::SmoothPixmapTransform| QPainter::TextAntialiasing);
        ui->gV->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        ui->gV->setVerticalScrollBarPolicy  (Qt::ScrollBarAlwaysOff);
        ui->gV->setTransformationAnchor(QGraphicsView::NoAnchor);
        ui->gV->setAutoFillBackground(false);
        ui->gV->setAttribute(Qt::WA_OpaquePaintEvent, true);
        ui->gV->setAttribute(Qt::WA_NoSystemBackground, true);
    
        resize(boardBaseSize);
    

    here is code to set the new scene to the view

    void GlScreenBoard::setShowScene(QGraphicsScene *scene, QString programName)
    {
         scene->setSceneRect(boardSceneRectBase);
    
         ui->gV->setScene(scene);
    
         ui->gV->scene()->addRect(ui->gV->scene()->sceneRect(),QColor("red"));
    
         QGraphicsTextItem *dd = ui->gV->scene()->addText(programName);
    
         dd->moveBy(10,boardBaseSize.height()/3);
         dd->setDefaultTextColor("red");
    }
    

    another problem is when I set Graphics view CacheMode to CacheBackground the OpenGL viewport disables !! and the painter in QGraphicsScene returns to Raster !


Log in to reply