Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error while trying to add QOpenGlWidget to a QWidget
Forum Updated to NodeBB v4.3 + New Features

Error while trying to add QOpenGlWidget to a QWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 710 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sara123
    wrote on last edited by
    #1

    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.
    problem.PNG

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi and welcome to the forums
      Im not sure GLWindow likes to be put inside another Widget

      Is there a reason you cant use
      https://doc.qt.io/qt-5/qopenglwidget.html#details

      Since it complains about the context.
      You could try to use
      QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
      and see if that makes it happy.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sara123
        wrote on last edited by
        #3

        @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_OBJECT

        public:
            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;
        

        };

        mrjjM 1 Reply Last reply
        0
        • S sara123

          @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_OBJECT

          public:
              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;
          

          };

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          0
          • gde23G Offline
            gde23G Offline
            gde23
            wrote on last edited by
            #5

            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)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved