PDA

View Full Version : How to render QGLWidget within QML



rannamal
20th May 2015, 10:00
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 :


#include <QtWidgets>
#include <QtOpenGL>
#include "glwidget.h"

GLWidget::GLWidget(QWidget *parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
qDebug() << __FUNCTION__;
}

GLWidget::~GLWidget()
{
qDebug() << __FUNCTION__;
}

QSize GLWidget::minimumSizeHint() const
{
qDebug() << __FUNCTION__;
return QSize(50, 50);
}

QSize GLWidget::sizeHint() const
{
qDebug() << __FUNCTION__;
return QSize(700, 700);
}

void GLWidget::initializeGL()
{
qDebug() << __FUNCTION__;
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}

void GLWidget::paintGL()
{
qDebug() << __FUNCTION__;
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glColor3f(0.0, 1.0, 0.0);
glRectf(-25.0, -25.0, 25.0, 25.0);
glPopMatrix();
}

void GLWidget::resizeGL(int w, int h)
{
qDebug() << __FUNCTION__;
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


glwidget.h :


#include <QGLWidget>

class GLWidget : public QGLWidget
{
Q_OBJECT

public:
GLWidget(QWidget *parent = 0);
~GLWidget();

QSize minimumSizeHint() const;
QSize sizeHint() const;

protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
};


main.cpp :


#include <QApplication>
#include <QDesktopWidget>
#include <QtQml>

#include "qtquick2applicationviewer.h"
#include "glwidget.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

qmlRegisterType<GLWidget>("com.opengl.display", 1, 0, "Opengl");
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("embed.qml"));
viewer.showFullScreen();

return app.exec();
}


embed.qml :


import QtQuick 2.0
import com.opengl.display 1.0

Item {
id: screen; width: 1080; height: 720
Rectangle {
id: background
anchors.fill: parent; color: "#343434";
Opengl {
id: openglwidget
}
}
}

anda_skoa
21st May 2015, 10:56
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,
_

rannamal
24th May 2015, 17:11
Thanks anda_skoa. I really appreciate for the pointers provided :).
Custom QQuickItem should solve what i am looking for.

AlekseyK
3rd November 2020, 09:25
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!