As the title suggests, inside of Qt, I am unable to use, or call any OpenGL functions outside of QGLWidget functions like initializeGL()
, and paintGL()
.
I am trying to build a 3D model, after a slot from my QMainWindow
class is called. Everything appears to work fine, all of the vertices are loaded, however, when I try and draw my model, I get a NULL reference exception; which occurs, most often, when too few Vertexes are provided.
The same building function works when I use it in initializeGL()
.
Asked
Active
Viewed 1,246 times
3

Cartier
- 429
- 4
- 15
1 Answers
7
This is actually quite simple: outside of those functions, the QGLWidget's context is not current. You can make the context current by calling QGLWidet::makeCurrent(). A perhaps better way to get around this is to defer calling any GL functions to within paintGL, as that ensures that the context is current, and you don't risk messing with any other openGL stuff by changing the context.

Jagoly
- 939
- 1
- 8
- 32
-
Thank you so much for this, I really do appreciate this. I have been searching for an answer for quite some time now! When you say it would be better to do everything inside of `paintGL()`, what would the advantages be? Thanks again! – Cartier Jul 05 '15 at 15:06
-
1Well, some Qt Widgets also use GL to draw. They too will call make their context(s) current, so it shouldn't be a problem. I just find that it makes more sense to me, I guess; the only place that GL objects will likely get used is within paintGL, so why not create them there as well? In the end, it's just up to how you design your application. – Jagoly Jul 05 '15 at 15:11
-
Oh! Alright, I understand. I will take that into consideration. Thanks again for the help. :) – Cartier Jul 05 '15 at 15:14
-
thanks a lot, I was searching for this problem during hours, my objects were painting fine if created in initializeGL, but not if elsewhere. – gxmad Jun 20 '19 at 14:58