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
){
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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
glwidget.h :
#include <QGLWidget>
{
Q_OBJECT
public:
~GLWidget();
QSize minimumSizeHint
() const;
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
};
#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);
};
To copy to clipboard, switch view to plain text mode
main.cpp :
#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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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
}
}
}
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
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks