Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] How do I set the QTOpenGLContext of a qml window before I run it.

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

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 2.3k 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.
  • M Offline
    M Offline
    Martell Malone
    wrote on last edited by
    #1

    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/

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #2

      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.

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Martell Malone
        wrote on last edited by
        #3

        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 ?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Martell Malone
          wrote on last edited by
          #4

          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

          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