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. QOpenGLWidget embedded in qml
Qt 6.11 is out! See what's new in the release blog

QOpenGLWidget embedded in qml

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 761 Views
  • 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
    Mavis
    wrote on last edited by Mavis
    #1

    Hello,
    I have a QOpenGLWidget class, how can it be embedded in qml? I use qmlRegisterType(), but I got a segmentation fault. Below it's some code.

    class MyGLWidget: public QOpenGLWidget
    {
    	Q_OBJECT
    public:
    	explicit MyGLWidget(QWidget* parent = NULL);
    	virtual ~MyGLWidget();
    }
    

    main.cpp

    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QApplication app(argc, argv);
    
        qmlRegisterType<MyGLWidget>("MyClass", 1, 0, "MyClass");
        QQuickView view(QUrl("qrc:/main.qml"));
        view.show();
    
        return app.exec();
    }
    

    main.qml:

    import QtQuick 2.8
    import MyClass 1.0
    
    Item {
        width: 800
        height: 600
    
        MyClass {
            id: glWindow
        }
    }
    

    QT version 5.14.2

    JoeCFDJ 1 Reply Last reply
    0
    • M Mavis

      Hello,
      I have a QOpenGLWidget class, how can it be embedded in qml? I use qmlRegisterType(), but I got a segmentation fault. Below it's some code.

      class MyGLWidget: public QOpenGLWidget
      {
      	Q_OBJECT
      public:
      	explicit MyGLWidget(QWidget* parent = NULL);
      	virtual ~MyGLWidget();
      }
      

      main.cpp

      int main(int argc, char *argv[])
      {
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
          QApplication app(argc, argv);
      
          qmlRegisterType<MyGLWidget>("MyClass", 1, 0, "MyClass");
          QQuickView view(QUrl("qrc:/main.qml"));
          view.show();
      
          return app.exec();
      }
      

      main.qml:

      import QtQuick 2.8
      import MyClass 1.0
      
      Item {
          width: 800
          height: 600
      
          MyClass {
              id: glWindow
          }
      }
      

      QT version 5.14.2

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #2

      @Mavis I guess you are trying to use opengl raw code in qml. If my guess is correct, you can create a
      class CustomOpenGLItem : public QQuickItem, protected QOpenGLFunctions
      and put your raw code inside and you can add this class in qml code.
      I do not think you are able to add QOpenGLWidget to qml directly.

       qmlRegisterType<CustomOpenGLItem>("CustomOpenGLItem", 1, 0, "CustomOpenGLItem");
      
      =============
      import QtQuick 2.15
      import CustomOpenGLItem 1.0
      
      ApplicationWindow {
          visible: true
          width: 400
          height: 300
          title: "OpenGL in QML Example"
      
          CustomOpenGLItem {
              anchors.fill: parent
          }
      }
      
      M 1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        @Mavis I guess you are trying to use opengl raw code in qml. If my guess is correct, you can create a
        class CustomOpenGLItem : public QQuickItem, protected QOpenGLFunctions
        and put your raw code inside and you can add this class in qml code.
        I do not think you are able to add QOpenGLWidget to qml directly.

         qmlRegisterType<CustomOpenGLItem>("CustomOpenGLItem", 1, 0, "CustomOpenGLItem");
        
        =============
        import QtQuick 2.15
        import CustomOpenGLItem 1.0
        
        ApplicationWindow {
            visible: true
            width: 400
            height: 300
            title: "OpenGL in QML Example"
        
            CustomOpenGLItem {
                anchors.fill: parent
            }
        }
        
        M Offline
        M Offline
        Mavis
        wrote on last edited by
        #3

        @JoeCFD Thanks. I'll study new class and transport my QOpenGLWidget class to QOpenGLFunctions.

        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