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

QPainter and QOpenGLWidget - Text Corruption



  • This kinda follows on from this post from a few years back:

    https://forum.qt.io/topic/64310/rendering-text-in-qopenglwidget

    When attempting to use QPainter to draw some text from within the paintGL function of a QOpenGLWidget, some kind of corruption is occurring on the font glyphs:

    746776a3-1911-427a-a9cb-084d463b54eb-image.png

    This text should just say "Hello there" but clearly does not.

    Within the paintGL function, I have the following code:

    QPainter p(this);
    p.setFont(QFont("Verdana", 16));
    p.setPen(Qt::white);
    p.setBrush(Qt::transparent);
    p->drawText(500, 500, 100, 20, Qt::AlignCenter, "Hello there");
    

    Any pointers would be greatly appreciated.


  • Lifetime Qt Champion

    When you're following this post then you're missing

    painter.beginNativePainting();

    but don't know if it help though.



  • @Christian-Ehrlicher unfortunately I’ve already tried that and it makes zero difference.


  • Lifetime Qt Champion

    Hi
    Using the code from the other post

    #include <QApplication>
    #include <QOpenGLWidget>
    #include <QOpenGLFunctions>
    #include <QPainter>
    
    class MyOpenGLWidget : public QOpenGLWidget
    {
    public:
        MyOpenGLWidget() {}
    
        void initializeGL()
        {
            QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
            f->glClearColor(1.f, 1.f, 1.f, 1.f);
        }
    
        void resizeGL(int w, int h)
        {
            QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
            f->glViewport(0, 0, w, h);
        }
    
        void paintGL()
        {
            QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
    
            f->glClear(GL_COLOR_BUFFER_BIT);
    
            QPainter painter(this);
            painter.setPen(Qt::black);
            painter.setFont(QFont("Arial", 56));
            painter.drawText(0, 0, width(), height(), Qt::AlignCenter, "Hello World!");
            painter.end();
        }
    };
    
    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
    
        MyOpenGLWidget wnd;
        wnd.resize(400, 300);
        wnd.show();
    
        return app.exec();
    }
    

    alt text

    Tried on win 10, win 7 and linux and had no text corruption.

    Where are you seeing this ?


Log in to reply