Question

It seems that the paintEvent method of the QGLWidget is called before initializeGL, so where am I supposed to put my openGL initialization code?

I'm putting it into the paintEvent method like this:

void MyGLWidget::paintEvent(...)
{
   makeCurrent();
   ..save modelview and projection matrices..

   // This is initialization code

   GLenum init = glewInit();
    if (GLEW_OK != init)
    {
      /* Problem: glewInit failed, something is seriously wrong. */
      qWarning() << glewGetErrorString(init);
    }

    // Dark blue background
    glClearColor(0.2f, 0.0f, 0.5f, 0.0f);
    // Enable depth test
    glEnable(GL_DEPTH_TEST);


   // End initialization code

   ... drawing code

   QPainter painter(this);
   ...overpainting..

}

I really don't like the idea of having my glew library initialization function called every time a paintEvent is raised... although that's working.

Any suggestion?

Was it helpful?

Solution

You have to initialize OpenGL in initializeGL(), there is no other option.

But you also have to draw inside paintGL, not inside paintEvent, so that is where your mistake is.

OTHER TIPS

Override initializeGL() function of QGLWidget. It is Created right for the purposes you want

From it's documentation:

This virtual function is called once before the first call to paintGL() or resizeGL(), and then once whenever the widget has been assigned a new QGLContext. Reimplement it in a subclass.

link to documentation: http://doc.qt.io/archives/qt-4.7/qglwidget.html#initializeGL

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top