Using QPainter and OpenGL together issues



  • I have been having a moderate of amount of success understanding QT. But I've run into a problem with mixing QPainter and openGL.

    I have written just a simple OpenGL renderer that compiles a simple vertex/fragment program. It draws a spinning textured cube. I then wanted to see some QPainter elements be drawn on top of the view and everything has stopped working.

    So here's the main rendering code:

    @void MainGL::paintEvent(QPaintEvent* event)
    {
    QPainter painter(this);
    painter.beginNativePainting();

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    

    // Clear color and depth buffer
    qglClearColor(m_bkcolor);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Calculate model view transformation
    QMatrix4x4 viewMatrix;
    viewMatrix.setToIdentity();
    viewMatrix.translate(0.0, 0.0, -5.0);
    viewMatrix.rotate(m_rotation);

    m_program.bind();
    

    m_program.setUniformValue("mvp_matrix", m_projection * viewMatrix);
    m_program.setUniformValue("texture", 0);
    m_cube.drawCubeGeometry(&m_program);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    painter.endNativePainting();

    // painter.begin(this);
    // painter.setPen(Qt::blue);
    // painter.setFont(QFont("Arial", 30));
    // painter.drawText(rect(), Qt::AlignCenter, "Hello World!");
    // painter.end();
    }
    @

    I've disabled the QPainter code except the painter.beginNativePainting() portion.

    My guess is, when I wrap the OpenGL code between the painter.beginNativePainting()/painter.endNativePainting(); that something is happening to my openGL context that I'm not sure what to do.

    You will notice I am binding the shader. I know that if this were a full-blown rendering pipeline, I would need to be sure my context was properly maintained. If I comment out the QPainter code, everything draws fine.

    What am I missing here?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.