Results 1 to 2 of 2

Thread: Issue placing a QOpenGLWidget in a QDockWidget

  1. #1
    Join Date
    Feb 2008
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Issue placing a QOpenGLWidget in a QDockWidget

    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 

  2. #2
    Join Date
    Feb 2008
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Issue placing a QOpenGLWidget in a QDockWidget

    The problem was related to the context getting destroyed and corrupting the QOpenGLFunctions base class. I found a work-around where the QOpenGLFunctions is used via a member variable instead of via multiple inheritance. The updated OpenGLWidget.h file is below.

    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{
    9. public:
    10. explicit OpenGLWidget(QWidget * pParent = nullptr) : QOpenGLWidget(pParent) {setFixedSize(200, 200);}
    11. ~OpenGLWidget() {DeleteFunctions();}
    12.  
    13. protected:
    14. void DeleteFunctions() {delete(m_pFunctions); m_pFunctions = nullptr;}
    15.  
    16. virtual void initializeGL() Q_DECL_OVERRIDE{
    17. connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &OpenGLWidget::DeleteFunctions);
    18.  
    19. m_pFunctions = new QOpenGLFunctions_4_3_Core();
    20. if(!m_pFunctions->initializeOpenGLFunctions()) qFatal("Failed to initialize the OpenGL functions.");
    21. glClearColor(0, 0, 0, 1);
    22. }
    23.  
    24. QOpenGLFunctions_4_3_Core * m_pFunctions = nullptr;
    25. };
    26.  
    27. #endif
    To copy to clipboard, switch view to plain text mode 
    Last edited by sandytf; 25th January 2015 at 16:26.

  3. The following user says thank you to sandytf for this useful post:

    Kryzon (25th January 2015)

Similar Threads

  1. QOpenGLWidget: do I need this?
    By rainbowgoblin in forum Qt Programming
    Replies: 1
    Last Post: 20th April 2014, 21:53
  2. Placing Clock on Title Bar
    By GG2013 in forum Newbie
    Replies: 2
    Last Post: 1st October 2013, 04:16
  3. Replies: 15
    Last Post: 16th August 2011, 22:18
  4. Issue in placing the QGraphicsItems into QGraphicsLayout
    By aditya.kaole in forum Qt Programming
    Replies: 4
    Last Post: 9th September 2010, 08:33
  5. Regarding placing images in the grid
    By prajna in forum Newbie
    Replies: 1
    Last Post: 20th February 2009, 09:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.