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. mixing QtOpenGL and QPaint programming

mixing QtOpenGL and QPaint programming

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 239 Views
  • 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.
  • L Offline
    L Offline
    limy
    wrote on last edited by
    #1

    I encountered a problem when mixing QtOpenGLWidget and QtPaint programming. I want to render 3D model with OpenGL and paint a text with qtpaint, but the text was not painted correctly, and was painted as points. My code is following, please help me how fix this problem.

    void GLWidget::paintGL()
    {
        //!OpenGL Draw
        QPainter painter(this);
        painter.beginNativePainting();
    //    painter.begin(this);
    
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //    glEnable(GL_BLEND);
    //    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    //    glEnable(GL_LINE_SMOOTH);
    
        //!render model
        coordPro->bind();
        int modelLoc = coordPro->uniformLocation("model");
        glUniformMatrix4fv(modelLoc, 1, GL_FALSE, m_world.data());
        VBO.bind();
        VAO.bind();
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr);
        glEnableVertexAttribArray(0);
        glLineWidth(2.0f);
        glDrawArrays(GL_LINES, 0, indexSize);
        VBO.release();
        VAO.release();
        coordPro->release();
    
        //!Draw Text
        glDisable(GL_BLEND);
        glDisable(GL_DEPTH_TEST);
        glDisable(GL_STENCIL_TEST);
        glDisable(GL_SCISSOR_TEST);
        QVector4D ndc = m_proj * m_camera * m_world * QVector4D(0.0f, 0.0f, 20.0f, 1.0f);
        //!pixels
        float x = (this->width() * 0.5f - 0.5f) * (ndc[0] + ndc[3]);
        float y = (this->height() * 0.5f - 0.5f) * (ndc[3] - ndc[1]);
        qDebug() << "x = " << x << "  y = " << y << "\n";
        QPen pen;
        pen.setColor(Qt::red);
        painter.setPen(pen);
        painter.drawText(x, y, QString("This is text"));//they are painted as POINTS, not text
        painter.endNativePainting();
    //    painter.end();
    }
    

    ![0_1575948722252_WeChat Image_20191210093147.png](Uploading 0%)

    1 Reply Last reply
    0
    • L Offline
      L Offline
      limy
      wrote on last edited by
      #2

      I have handled the problem. I set the render polygon point mode when rending model with OpenGL, so the text was rendered as points.

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wrosecrans
        wrote on last edited by
        #3

        @limy said in mixing QtOpenGL and QPaint programming:

        Try doing endNativePainting(); before you start doing QPainter stuff, and it'll restore the QPainter state. Actually, I think you probably want to do your OpenGL painting first and once that's done then create the QPainter and paint with it so you don't need to interleave the painter and OpenGL.

        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