Advertisement Jump to content
Sign in to follow this  

[Qt] Trying to get QGraphicsView and OpenGL to play nice

This topic is 3880 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I've been tearing my hair out over the past view days getting Qt to render some OpenGL inside a QGraphicsView widget. The documentation claims the following:
To render using OpenGL, simply call setViewport(new QGLWidget). QGraphicsView takes ownership of the viewport widget.
I'm absracting QGraphicsScene and reimplementing QGraphicsScene::drawForeground() in which to call my OpenGL stuff. For the past two days all I've been getting is a white window. Here is the relevant parts of the code: Header:
class Qt_OGLScene : public QGraphicsScene

		Qt_OGLScene (QWidget* parent = 0);
		~Qt_OGLScene ();

		void drawForeground( QPainter* painter, const QRectF & rect );

void Qt_OGLScene::drawForeground( QPainter* painter, const QRectF & rect )
	glColor3f (1.0, 1.0, 1.0);
	glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
		glVertex3f (0.25, 0.25, 0.0);
		glVertex3f (0.75, 0.25, 0.0);
		glVertex3f (0.75, 0.75, 0.0);
		glVertex3f (0.25, 0.75, 0.0);

Main window setup:
Qt_OGLScene GLScene;

Qt_MainWindow::Qt_MainWindow(QWidget *parent)

    Preview->setViewport(new QGLWidget);

I've been reading countless articles and browsing example code, and I can't get it to function. Thanks in advance for any help. EDIT: Forgot to mention, 'Preview' is the QGraphicsView widget, set up in the Qt4 Designer.

Share this post

Link to post
Share on other sites
Forget it, I found the problem just after posting. Typical.

My OpenGL 'test' code wasn't valid... can't believe I was so dumb.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!