Results 1 to 4 of 4

Thread: How to render QGLWidget within QML

  1. #1

    Default How to render QGLWidget within QML

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to render QGLWidget within QML

    You cannot embed a widget into a QtQuick2 scene.

    If you want to render items in the scene with OpenGL, you can create a cusomt QQuickItem or use a QQuickFramebufferObject to render into that and have it displayed in the scene.

    Cheers,
    _

  3. #3

    Default Re: How to render QGLWidget within QML

    Thanks anda_skoa. I really appreciate for the pointers provided .
    Custom QQuickItem should solve what i am looking for.

  4. #4
    Join Date
    Nov 2010
    Posts
    47
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to render QGLWidget within QML

    Quote Originally Posted by rannamal View Post
    Thanks anda_skoa. I really appreciate for the pointers provided .
    Custom QQuickItem should solve what i am looking for.
    Is there any good example how to transform QGLWidget to custom QQuickItem? Thanks!

Similar Threads

  1. Render QWidget ontop of QGLWidget
    By scarecr0w132 in forum Newbie
    Replies: 1
    Last Post: 13th November 2013, 09:24
  2. Qt4 QGLwidget::renderPixmap don't render texture
    By Glandelf in forum Qt Programming
    Replies: 1
    Last Post: 5th May 2012, 20:20
  3. QGLWidget render or QGraphicsView with GL viewport render
    By QTInfinity in forum Qt Programming
    Replies: 2
    Last Post: 28th November 2011, 11:34
  4. Render QWidget within QGLWidget
    By crazymonkey in forum Newbie
    Replies: 29
    Last Post: 26th September 2010, 13:54
  5. QGLWidget Render Text in the foreground
    By arpspatel in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2010, 11:35

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.