Solved Calling OpenGL functions outside InitializeGL / PaintGL
-
I'm using a 3rd party rendering library, and when I try to load a model from the menu bar, it crashes.
The way I'm set up is having a QMainWindow as the main window of the application. Inside this window, I have both the menu bar, and a QDockWidget that contains a QOpenGLWidget.
The QOpenGLWidget handles GL initialization and painting via initializeGL and paintGL, respectively.If I place my model loading code manually inside either of these functions (initializeGL and paintGL), then everything works correctly.
However, if I bind the model load function to the QMainWindow via the creation of a corresponding QMenu and QAction, then the application crashes.
In fact, placing the model loading code anywhere outside initializeGL or paintGL functions, the application crashes.Is there a way to load a model outside the QOpenGLWidget without restructuring the entire application and 3D engine?
Alternatively, how would I go about restructuring the application while retaining the functionality of having my 3D viewport inside a QDockWidget, and still be able to call GL functions? -
Hi and welcome to devnet,
When these methods are called you have a valid OpenGL context to work with, which is not the case otherwise.
When exactly would you like to load your 3D models ?
-
For example, from a QAction in a QMenu, for example, an Edit menu with a Load Model action.
Since the QMenuBar and it's subsequent QMenu and QAction children use the signal connect syntax, I can't connect them to a GL function on the QOpenGLWidget. -
Then what about setting a flag stating that there's a new model to load, then in paintGL check that flag, load the model and clear it once done ?
-
@SGaist said in Calling OpenGL functions outside InitializeGL / PaintGL:
Then what about setting a flag stating that there's a new model to load, then in paintGL check that flag, load the model and clear it once done ?
Yes, that is definitely one approach to consider.
Is it not possible to do it any other way?
I'd much prefer if it was somehow possible to call GL functions outside of these 2 specific functions, but if impossible I'd fall back to the method you suggested. -
You can't call OpenGL function at any time you want but that's not Qt specific. You have have to have a valid context and make it current. You can take a look at the Qt OpenGL examples in the sources to get more ideas.
-
@SGaist
Thanks. I was hoping it's possible, but I guess I'll have to make do with this workaround.