I'm having an issue placing a subclassed QOpenGLWidget inside a QDockWidget and was wondering if anyone else is experiancing a similar problem. When the example code below is run, the QMainWindow and the QDockWidget appear as expected. However, once the QDockWidget is dragged over the QMainWindow and docked, there is an unhandled exception. I think the problem may involve the OpenGL context changing or getting destroyed. When debugging with Visual Studio 2013 Update 4, the debugger breaks into QOpenGLFunctions_4_3_Core::glClearColor and the d_1_0_core variable appears to be invalid. I tried making the OpenGLWidget class use QOpenGLFunctions_4_3_Core as a member variable (instead of using inheritance) and recreating it each time initializeGL was called, but that seemed to just make the problem worse.

System: Windows 7 64-bit
Compiler: Visual Studio 2013 Update 4
Configuration: 32-bit debug
Qt: 5.4

MainWindow.h
Qt Code:
  1. #ifndef __MAIN_WINDOW__
  2. #define __MAIN_WINDOW__
  3.  
  4. // Forward declarations
  5. class OpenGLWidget;
  6.  
  7. // System includes
  8. #include <QMainWindow>
  9.  
  10. class MainWindow : public QMainWindow{
  11. Q_OBJECT
  12.  
  13. public:
  14. MainWindow(QWidget * pParent = nullptr);
  15.  
  16. protected:
  17. OpenGLWidget * m_pOpenGLWidget = nullptr;
  18. QDockWidget * m_pDockWidget = nullptr;
  19. };
  20.  
  21. #endif
To copy to clipboard, switch view to plain text mode 

MainWindow.cpp
Qt Code:
  1. #include "MainWindow.h"
  2.  
  3. // System includes
  4. #include <QDockWidget>
  5.  
  6. // Application includes
  7. #include "OpenGLWidget.h"
  8.  
  9. MainWindow::MainWindow(QWidget * pParent) : QMainWindow(pParent){
  10. m_pOpenGLWidget = new OpenGLWidget(this);
  11. m_pDockWidget = new QDockWidget("OpenGL Dock", this);
  12. m_pDockWidget->setWidget(m_pOpenGLWidget);
  13. m_pDockWidget->setFloating(true);
  14. resize(300, 300);
  15. }
To copy to clipboard, switch view to plain text mode 

OpenGLWidget.h
Qt Code:
  1. #ifndef ___OPENGL_WIDGET___
  2. #define ___OPENGL_WIDGET___
  3.  
  4. // System includes
  5. #include <QOpenGLWidget>
  6. #include <QOpenGLFunctions_4_3_Core>
  7.  
  8. class OpenGLWidget : public QOpenGLWidget, protected QOpenGLFunctions_4_3_Core{
  9. public:
  10. explicit OpenGLWidget(QWidget * pParent = nullptr) : QOpenGLWidget(pParent) {setFixedSize(200, 200);}
  11.  
  12. protected:
  13. virtual void initializeGL() Q_DECL_OVERRIDE{
  14. if(!initializeOpenGLFunctions()) qFatal("Failed to initialize the OpenGL functions.");
  15. glClearColor(0, 0, 0, 1);
  16. }
  17. };
  18.  
  19. #endif
To copy to clipboard, switch view to plain text mode 

Main.cpp
Qt Code:
  1. // System includes
  2. #include <QApplication>
  3.  
  4. // Application includes
  5. #include "MainWindow.h"
  6.  
  7. int main(int argc, char ** argv){
  8. QApplication app(argc, argv);
  9. MainWindow mainWindow;
  10.  
  11. mainWindow.show();
  12. return(app.exec());
  13. }
To copy to clipboard, switch view to plain text mode