PDA

View Full Version : OpenGL Render issue when using QGLWidget with QGraphicsView



outburstx
5th May 2010, 23:34
I created a simple QGraphicsView-inherited class that uses a QGLWidget-inherited object as a viewport.

I want the contents of the QGLWidget to be rendered as the background of the QGraphicsView.

Sounds simple, right?

It works fine as long as I don't add any QGraphicItems to the scene -- the graphic that I have defined in my QGLWidget gets drawn as the background of the QGraphicsView.
However, as soon as I add an item to the scene, the background does not render properly.

Everything works smoothing if my QGLWidget renders in direct mode (e.g. glBegin(), glEnd()). However, if I use vertex arrays or VBOs, I get weird anomalies in my render.

My theory is that QGraphicsView is modifying the OpenGL context somewhere and not resetting its state, such that when I go to render in my QGLWidget, I am in an unexpected state.

Can someone please try the code I have posted here?
It uses the "GLWidget" class from <qt-dir>/examples/opengl/hellogl/qlwiget.* , and that in turns relies on <qt-dir>/examples/opengl/share/qtlogo.*

If you comment out the 'addEllipse' line, you should see the Qt graphic as the background, but if you keep it uncommented, you should see something wonky.

I'm using qt4.6.2 and Fedora11 64-bit on a higher-end NVIDIA card.


#include <QtGui>
#include "glwidget.h" // from <qt-dir>/examples/opengl/hellogl/glwidget.*

// QGraphicsView that uses a GLWidget as its viewport
class GLGraphicsView : public QGraphicsView
{
public:
GLGraphicsView(QGraphicsScene * scene, QWidget * parent = 0 ) : QGraphicsView(scene, parent)
{
mpGLWidg = new GLWidget(NULL);
setViewport(mpGLWidg);
};

protected:

void drawBackground(QPainter *p, const QRectF &r)
{
mpGLWidg->updateGL();
QGraphicsView::drawBackground(p, r);
};
private:
GLWidget* mpGLWidg; // use GLWidget class from <qt-dir>/examples/opengl/hellogl/glwidget.*
// it depends on <qt-dir>/examples/opengl/shared/qtlogo.*
};


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

QGraphicsScene scene;
GLGraphicsView gView(&scene);

// comment this line out, and the logo will appear:
scene.addEllipse(QRectF(10, 20, 30, 40), QPen(Qt::green), QBrush(Qt::SolidPattern));

gView.resize(800,600);
gView.show();

int ret = app.exec();
return ret;
}

outburstx
7th May 2010, 17:20
For anyone who runs into this problem, I suggest taking a look at the 'boxes' demo: http://doc.trolltech.com/4.6//demos-boxes.html

The design I have above is slightly skewed, and the demo shows a much better way to go about putting an OpenGL background behind a bunch of Qt widgets.
(basically, you inherit from QGraphicsScene instead of QGLWidget to add custom OpenGL code)