相关文章推荐
听话的煎饼  ·  【Windows】Windows ...·  2 年前    · 
威武的馒头  ·  MSSQLSERVER_4064 - ...·  2 年前    · 
沉着的水桶  ·  [GNU/Emacs] ...·  3 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

If I call "updateGL" from paintEvent, and let initializeGL and paintGL do the work, for some reason the screen is rendered okay but then is immediately swapped entirely white. Why is that? – Johnny Pauling Jul 18, 2012 at 19:52

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

as I said, it seems initializeGL is called AFTER paintEvent, so I can't draw in paintEvent without glew extensions and shaders set (which should be done, in my humble opinion, into initializeGL) – Johnny Pauling Jul 18, 2012 at 17:25 @JohnPell: sorry, I did not notice that. Are you sure it's called after paint event? Try to put break points in both functions and check which one is triggered first – Andrew Jul 18, 2012 at 17:37 I did that, that's why I wrote that initializeGL is called after paintEvent. It's weird but seems so – Johnny Pauling Jul 18, 2012 at 17:46 @JohnPell: have you tried putting breakpoints to see if paint is really called before initialize ? – Andrew Jul 18, 2012 at 17:48 yes, I tried with breakpoints, and it's not necessary to put breakpoints because if I put the initialization code into initializeGL and the normal drawing code in paintEvent, the scene crashes because initializeGL isn't called before paintEvent – Johnny Pauling Jul 18, 2012 at 17:49

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.