Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

mixing QtOpenGL and QPaint programming



  • 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%)



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



  • @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.


Log in to reply