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 :
Code:
#include <QtWidgets>
#include <QtOpenGL>
#include "glwidget.h"
GLWidget
::GLWidget(QWidget *parent
){
qDebug() << __FUNCTION__;
}
GLWidget::~GLWidget()
{
qDebug() << __FUNCTION__;
}
QSize GLWidget
::minimumSizeHint() const {
qDebug() << __FUNCTION__;
}
QSize GLWidget
::sizeHint() const {
qDebug() << __FUNCTION__;
}
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 :
Code:
#include <QGLWidget>
{
Q_OBJECT
public:
~GLWidget();
QSize minimumSizeHint
() const;
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
};
main.cpp :
Code:
#include <QApplication>
#include <QDesktopWidget>
#include <QtQml>
#include "qtquick2applicationviewer.h"
#include "glwidget.h"
int main(int argc, char *argv[])
{
qmlRegisterType<GLWidget>("com.opengl.display", 1, 0, "Opengl");
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("embed.qml"));
viewer.showFullScreen();
return app.exec();
}
embed.qml :
Code:
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
}
}
}
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,
_
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.
Re: How to render QGLWidget within QML
Quote:
Originally Posted by
rannamal
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!