I am trying to show QGLWidget within QML. But somehow its not visible. Can someone point what might have gone wrong. The same glwidget works fine with Qt instead of QML. When the instance of glwidget is created within qml it calls the corresponding constructor but none of the gl call gets called.
Can't I show a QGLWidget within QML ? If not how can i render opengl widgets within QML ?

Here is a sample code which reproduces the issue. I am trying to render the GLWidget withing embed.qml

glwidget.cpp :
Qt Code:
  1. #include <QtWidgets>
  2. #include <QtOpenGL>
  3. #include "glwidget.h"
  4.  
  5. GLWidget::GLWidget(QWidget *parent)
  6. : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
  7. {
  8. qDebug() << __FUNCTION__;
  9. }
  10.  
  11. GLWidget::~GLWidget()
  12. {
  13. qDebug() << __FUNCTION__;
  14. }
  15.  
  16. QSize GLWidget::minimumSizeHint() const
  17. {
  18. qDebug() << __FUNCTION__;
  19. return QSize(50, 50);
  20. }
  21.  
  22. QSize GLWidget::sizeHint() const
  23. {
  24. qDebug() << __FUNCTION__;
  25. return QSize(700, 700);
  26. }
  27.  
  28. void GLWidget::initializeGL()
  29. {
  30. qDebug() << __FUNCTION__;
  31. glClearColor (0.0, 0.0, 0.0, 0.0);
  32. glShadeModel (GL_FLAT);
  33. }
  34.  
  35. void GLWidget::paintGL()
  36. {
  37. qDebug() << __FUNCTION__;
  38. glClear(GL_COLOR_BUFFER_BIT);
  39. glPushMatrix();
  40. glColor3f(0.0, 1.0, 0.0);
  41. glRectf(-25.0, -25.0, 25.0, 25.0);
  42. glPopMatrix();
  43. }
  44.  
  45. void GLWidget::resizeGL(int w, int h)
  46. {
  47. qDebug() << __FUNCTION__;
  48. glViewport (0, 0, (GLsizei) w, (GLsizei) h);
  49. glMatrixMode(GL_PROJECTION);
  50. glLoadIdentity();
  51. glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
  52. glMatrixMode(GL_MODELVIEW);
  53. glLoadIdentity();
  54. }
To copy to clipboard, switch view to plain text mode 

glwidget.h :
Qt Code:
  1. #include <QGLWidget>
  2.  
  3. class GLWidget : public QGLWidget
  4. {
  5. Q_OBJECT
  6.  
  7. public:
  8. GLWidget(QWidget *parent = 0);
  9. ~GLWidget();
  10.  
  11. QSize minimumSizeHint() const;
  12. QSize sizeHint() const;
  13.  
  14. protected:
  15. void initializeGL();
  16. void paintGL();
  17. void resizeGL(int width, int height);
  18. };
To copy to clipboard, switch view to plain text mode 

main.cpp :
Qt Code:
  1. #include <QApplication>
  2. #include <QDesktopWidget>
  3. #include <QtQml>
  4.  
  5. #include "qtquick2applicationviewer.h"
  6. #include "glwidget.h"
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10. QApplication app(argc, argv);
  11.  
  12. qmlRegisterType<GLWidget>("com.opengl.display", 1, 0, "Opengl");
  13. QtQuick2ApplicationViewer viewer;
  14. viewer.setMainQmlFile(QStringLiteral("embed.qml"));
  15. viewer.showFullScreen();
  16.  
  17. return app.exec();
  18. }
To copy to clipboard, switch view to plain text mode 

embed.qml :
Qt Code:
  1. import QtQuick 2.0
  2. import com.opengl.display 1.0
  3.  
  4. Item {
  5. id: screen; width: 1080; height: 720
  6. Rectangle {
  7. id: background
  8. anchors.fill: parent; color: "#343434";
  9. Opengl {
  10. id: openglwidget
  11. }
  12. }
  13. }
To copy to clipboard, switch view to plain text mode