[SOLVED] Creating a QQuickView with a shared opengl context.



  • How can i create a QQuickView which shares it's context with another?



  • It's not really an elegant solution but i think i found one.

    Before setting QML file, (setSource) connect a slot to QQuickWindow::sceneGraphInitialized() signal like this :
    @connect(this, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);@

    when it hits the slot, do following :

    @
    openglContext()->blockSignals(true);
    openglContext()->doneCurrent();
    openglContext()->setShareContext(openGLContextToBeSharedWith);
    openglContext()->setFormat(requestedFormat());
    openglContext()->create();
    openglContext()->makeCurrent(this);
    openglContext()->blockSignals(false); // <- Forgot this line before
    @



  • hi Turgut,

    How did you display it after extending the class?



  • As it was.
    @
    class MyView : QQuickView
    {
    Q_OBJECT
    public :
    MyView(QWindow *parent = 0) : QQuickView(parent)
    {
    connect(this, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);

      setPersistentOpenGLContext(true);
      backgroundContext = new QOpenGLContext(this);
      backgroundContext->setFormat(requestedFormat());
      backgroundContext->create();
      backgroundContext->makeCurrent(this);
    
      static bool openGLExtensionsInitialized = false;
      if (!openGLExtensionsInitialized) {
        OpenGL::initialize(backgroundContext);
        openGLExtensionsInitialized = true;
      }
    
      backgroundContext->doneCurrent();
    
    }
    

    protected slots :
    onSceneGraphInitialized() {
    openglContext()->blockSignals(true);
    openglContext()->doneCurrent();
    openglContext()->setShareContext(backgroundContext);
    openglContext()->setFormat(requestedFormat());
    openglContext()->create();
    openglContext()->makeCurrent(this);
    openglContext()->blockSignals(false);
    }
    protected :
    QOpenGLContext *backgroundContext;
    };

    void someInitFunction()
    {
    MyView *view = new MyView();
    view->setSource(QUrl("qrc:/script/init.qml"));
    view->show();
    app.exec();
    }
    @



  • Many Thanks Turgut :)
    I noticed what I was missing


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.