Important: Please read the Qt Code of Conduct -

Not receving QOpenGLContext::aboutToBeDestroyed signal on exit

  • Hi,

    I'm porting a fully functional QGLWidget based application to use QOpenGLWidget instead and I'm hitting some resource tidying up issues since there appear to differences in the way that the two close down.

    Looking at the documentation, the way to guarantee that you get a notification when the context is still valid is to hook into the QOpenGLContext::aboutToBeDestroyed signal.

    So in my derived class (COpenGlWnd) I have this:

    void COpenGlWnd::initializeGL()
        connect(context(), &QOpenGLContext::aboutToBeDestroyed,
                this, &COpenGlWnd::contextAboutToBeDestroyed);
        // Other intialisation stuff

    However the slot (contextAboutToBeDestroyed) is never called when I exit the application so I can't clean up properly and get a lot of OpenGl warnings as I shut down and try to delete resources (display lists, textures etc) because the context is no longer valid. These could of course be ignored since I am shutting down anyway, but I don't want to get in the habit of ignoring warnings that might indicate other faults.

    I am quite familiar with Qt as well as signals and slots and everything seems to be in order otherwise.

    Can anyone tell me what I am doing wrong or whether this signal simply isn't issued during shutdown, in which case what am I supposed to do?



  • This is an old thread, but I was hitting this issue as well. The documentation is highly misleading - I suspect that QOpenGLContext::aboutToBeDestroyed is only sent when the context is being destroyed while QOpenGLWidget is staying alive. If QOpenGLWidget is being destroyed, the signal isn't sent.

    This actually makes sense if you think about the destruction order (signal would have to be triggered within QOpenGLWidget's destructor, which would be damaging since derived classes have already been destroyed).

    My solution: Connect to QCoreApplication::aboutToQuit. I actually connect to both signals. Not sure if there's a case where both will fire, so I check in my slot whether resources have already been cleaned.

    	QOpenGLContext* context = QOpenGLContext::currentContext();
    	connect(context, &QOpenGLContext::aboutToBeDestroyed, this, &MyClass::OnAboutToBeDestroyed);
    	// Unintuitive!  QOpenGLContext::aboutToBeDestroyed is apparently not send on program termination,
    	//	so we also connect to application quit. 
    	connect(qApp, &QCoreApplication::aboutToQuit, this, &MyClass::OnAboutToBeDestroyed);

Log in to reply