1

似乎QGLWidget的paintEvent方法是在initializeGL之前调用的,那么我应该把我的openGL初始化代码放在哪里呢?

我将它放入这样的paintEvent方法中:

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

}

我真的不喜欢每次引发 paintEvent 时都调用我的 glew 库初始化函数的想法......虽然这很有效。

有什么建议吗?

4

2 回答 2

2

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.

于 2012-07-18T19:04:23.990 回答
0

覆盖 QGLWidget 的 initializeGL() 函数。它是为您想要的目的而创建的

从它的文档:

这个虚函数在第一次调用paintGL() 或resizeGL() 之前被调用一次,然后每当widget 被分配一个新的QGLContext 时调用一次。在子类中重新实现它。

文档链接: http: //doc.qt.io/archives/qt-4.7/qglwidget.html#initializeGL

于 2012-07-18T17:24:16.190 回答