Important: Please read the Qt Code of Conduct -

QPainter and QOpenGLWidget - Text Corruption

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

    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:


    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->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


    but don't know if it help though.

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

  • Lifetime Qt Champion

    Using the code from the other post

    #include <QApplication>
    #include <QOpenGLWidget>
    #include <QOpenGLFunctions>
    #include <QPainter>
    class MyOpenGLWidget : public QOpenGLWidget
        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();
            QPainter painter(this);
            painter.setFont(QFont("Arial", 56));
            painter.drawText(0, 0, width(), height(), Qt::AlignCenter, "Hello World!");
    int main(int argc, char **argv)
        QApplication app(argc, argv);
        MyOpenGLWidget wnd;
        wnd.resize(400, 300);;
        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