QGLWidget ignores glClear

1.5k views Asked by At

I'm stumped. I have a widget inside the mainwindow on a QT 4.6 application which has been configured as a openGL widget. It draws just fine except I am unable to clear the background between frames. I get a black background when he window opens and it never clears after that, so I get a jumbled mess of rendered objects as time goes along. I'm also having to call swapBuffers() at the end of the paintGL() function to get the widget to show the most recent frame which has me puzzled as I was under the impression that swapBuffers() was called automatically. I am set up for double buffering and the widget is not shared. Here is the relevant code:

void GLWidget::paintGL ( )
{
    m_Input.Draw();
    QGLWidget::swapBuffers();
}

void GLWidget::initializeGL ( )
{
    qglClearColor(QColor(0,0,255,128));
    glClear(GL_COLOR_BUFFER_BIT);
}

It does seem there's something not right with the double buffering. Clearing the screen to a background color is pretty basic. But it's driving me nuts as to why it's not working. The remainder of the drawing code is working fine. Any ideas? (This is on a Linux system.)

1

There are 1 answers

5
datenwolf On

glClear is a drawing operation. It doesn't belong into initializeGL but into paintGL. The clearing color should be set right before calling glClear as well, so move that [q]glClearColor along.

Update

The paintGL method should look like this:

void GLWidget::paintGL()
{
    qglClearColor(QColor(0,0,255,128));
    glClear(GL_COLOR_BUFFER_BIT);
    // you probably also want to clear the depth and stencil buffers
    // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    m_Input.Draw();

    QGLWidget::swapBuffers();
}

Of course you must make sure that m_Input.Draw() doesn't mess things up again. You didn't show the code for that, so I'm in the blind here.