[SOLVED] How do I set the QTOpenGLContext of a qml window before I run it.



  • Hi Guys,

    I have the following code.

    @
    QQmlEngine engine;
    QQmlComponent component(&engine);
    component.loadUrl(QUrl("qrc:/qml/main.qml"));
    if ( !component.isReady() ) {
    qWarning("%s", qPrintable(component.errorString()));
    return -1;
    }

    QObject *topLevel = component.create();
    QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
    
    if ( !window ) {
        qWarning("Error: Your root item has to be a Window.");
        return -1;
    }
    window->show();
    

    @

    How do I set the context of the QWindow to a custom QOpenGLContext?

    I tried to Extend QWindow via EditorWindow
    but I get the error Your root item has to be a Window.

    I have seen this but cant quite implement it.
    http://qt-project.org/forums/viewthread/25268/



  • You don't set the context itself. Instead, request a custom context that matches what you wish by creating a QSurfaceFormat, setting options on it as needed and calling setFormat() in the ctor of a QQuickWindow subclass.



  • Ok so now I have this.

    @
    #ifndef MyWindow_h
    #define MyWindow_h

    #include <QtQuick/QQuickWindow>

    class MyWindow : public QQuickWindow {
    Q_OBJECT

    public:
    MyWindow(QWindow* parent = 0);

    };

    #endif
    @

    and

    @

    #include "MyWindow.h"

    #include <QtCore/QTimer>

    MyWindow::MyWindow(QWindow* parent)
    : QQuickWindow(parent)
    {

    QSurfaceFormat m_format;
    m_format.setMajorVersion(2);
    m_format.setMinorVersion(0);
    
    m_format.setDepthBufferSize(24);
    m_format.setAlphaBufferSize(8);
    m_format.setRedBufferSize(8);
    m_format.setGreenBufferSize(8);
    m_format.setBlueBufferSize(8);
    m_format.setSamples(4);
    m_format.setStencilBufferSize(8);
    m_format.setOption(QSurfaceFormat::StereoBuffers);
    
    //really need this?
    setFormat(m_format);
    
    QOpenGLContext m_context(this)
    m_context.setFormat(m_format);
    
    if (!m_context.create())
        qFatal("Cannot create the requested OpenGL context!");
        m_context.makeCurrent(this);
    

    }
    @

    How do I get this as my root window for the qml component ?



  • Hi guys think I have solved this for anyone else looking to do it.

    @

    QGuiApplication app( argc, argv );

    MyWindow window;
    QQmlEngine engine;
    QQmlComponent component(&engine, QUrl::fromLocalFile&#40;QLatin1String("qml/main.qml"&#41;&#41;);
    
    QQuickItem *item = qobject_cast<QQuickItem*>(component.create());
    item->setParentItem(window.contentItem());
    qDebug() << item;
    
    window.show();
    
    return app.exec&#40;&#41;
    

    @

    you cast back to a qquickitem and set our window as the parent like so.

    Thanks again zapB


Log in to reply
 

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