Custom OpenGL rendering in a QWidget
-
I want to render OpenGL in a QWidget but I don't want to use Qt's built-in tools like QOpenGLWidget because :
- I want to have a graphics API abstraction (vulkan, directX) to render in a QWidget
- I want to use a minimum of Qt only in a QWidget because the rendering methods must belong to an external library that does not use Qt (possible use of frameworks other than Qt later).
For the moment I'm just trying to get a working example with OpenGL (with glew and OpenGL functions), but I can't see how to render cleanly in a QWidget.
class RenderWidget : public QWidget { Q_OBJECT public: /** Constructors **/ explicit RenderWidget(QWidget* parent = nullptr); ~RenderWidget(); protected: /** Methods **/ virtual void initialize(); virtual void paint(); virtual void resize(int width, int height); void paintEvent(QPaintEvent *event) override; void resizeEvent(QResizeEvent *event) override; private: GLuint vertexBuffer; // Un exemple de tampon de vertex GLuint program; // Un exemple de programme de shader };
At the moment, calling glewInit() in the RenderWidget constructor fails, I've certainly misdeclared my OpenGL context (I used QSurfaceFormat from main).
I've seen topics saying that you shouldn't call OpenGL rendering functions directly from QWidget's paintEvent().
Do you have any ideas on how to do this properly? Do you have any examples of custom OpenGL (or other API) integration in Qt?
Thanks.
-
Hi,
You should rather take a look at QWindow.
That said, Qt already provides integration for many of the modern graphics API such as Direct3D or Vulkan. You might want to explore the scene graph documentation and the corresponding classes. Engines such as Ogre3D have integration that might of interest.
-
@julienchz
Hi
I have created some opengl tutorials check here: https://bitbucket.org/joaodeusmorgado/opengltutorials/src/master/
I am using QOpenGLWidget, but I use mostly raw opengl, you could easily create a abstract render class, and then reuse it to QOpenGLWidget, or any other frame work of your choice render class.
Hope it helps