Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using QPainter and OpenGL together issues
Forum Updated to NodeBB v4.3 + New Features

Using QPainter and OpenGL together issues

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 847 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kloveridge
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved