Error while trying to add QOpenGlWidget to a QWidget
-
Hello!
I am trying to implement QopenglWidget inside a QWidget by using the addWidget() function to insert the openGl window into the layout. Unfortunately the program crashes and I get this error. Can someone explain me what the reason behind this might be..
This is the constructor of the Qwidget where I try to insert the opengl widget.
game::game()
{GlWindow_obj = new GlWindow(this); GlWindow_obj->installEventFilter(this); KeyPressesHandler_obj = new KeyPressesHandler; QGridLayout *layout = new QGridLayout; layout->addWidget(GlWindow_obj, 0, 1); setLayout(layout);
}
Thank you in advance.
-
Hi and welcome to the forums
Im not sure GLWindow likes to be put inside another WidgetIs there a reason you cant use
https://doc.qt.io/qt-5/qopenglwidget.html#detailsSince it complains about the context.
You could try to use
QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
and see if that makes it happy. -
@sara123 said in Error while trying to add QOpenGlWidget to a QWidget:
GlWindow
Thank you very much.
Actually there is a mistake in the naming that I am using for my openGl subclass, as I called it GlWindow. This is because I used to use QOpenGlWindow at the beginning but I changed it at some point to QOpenGlWidget as seen in the following code:class GlWindow : public QOpenGLWidget, protected QOpenGLFunctions
{
Q_OBJECTpublic: GlWindow(QWidget *parent = nullptr); ~GlWindow(); void Init(); void PaintTetrisShape(QVector<TetrisShape*> container); void PaintSmth(); private: TetrisShape *shape; QVector<TetrisShape*> _shapeContainer; protected: void resizeGL(int width, int height) override; void paintGL() override; void initializeGL() override;
};
-
@sara123 said in Error while trying to add QOpenGlWidget to a QWidget:
GlWindow
Thank you very much.
Actually there is a mistake in the naming that I am using for my openGl subclass, as I called it GlWindow. This is because I used to use QOpenGlWindow at the beginning but I changed it at some point to QOpenGlWidget as seen in the following code:class GlWindow : public QOpenGLWidget, protected QOpenGLFunctions
{
Q_OBJECTpublic: GlWindow(QWidget *parent = nullptr); ~GlWindow(); void Init(); void PaintTetrisShape(QVector<TetrisShape*> container); void PaintSmth(); private: TetrisShape *shape; QVector<TetrisShape*> _shapeContainer; protected: void resizeGL(int width, int height) override; void paintGL() override; void initializeGL() override;
};
@sara123
Hi
Ah, so it is the widget version.
Then I see no reason it should crash being inserted into a layout.If you just create an instance of it, does it then work fine?
Im wondering if its all setup as expected since it crashes here
static QOpenGLFunctionsPrivateEx *qt_gl_functions(QOpenGLContext *context = 0) { if (!context) context = QOpenGLContext::currentContext(); Q_ASSERT(context); QOpenGLFunctionsPrivateEx *funcs = qt_gl_functions_resource()->value<QOpenGLFunctionsPrivateEx>(context); return funcs; }
So it seems related to getting the context.
-
It seems that you did not init you openGL functions corretyl.
Did you call initializeOpenGLFunctions(); somewhere int the init/constructor?As a test you could also take the OpenGlWidget example from Qt creator and set it to your layout to see if that runs. (Cube OpenGL Example)