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. QPainter and QOpenGLWidget - Text Corruption
Forum Updated to NodeBB v4.3 + New Features

QPainter and QOpenGLWidget - Text Corruption

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 388 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.
  • webzoidW Offline
    webzoidW Offline
    webzoid
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      painter.beginNativePainting();

      but don't know if it help though.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      webzoidW 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

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

        painter.beginNativePainting();

        but don't know if it help though.

        webzoidW Offline
        webzoidW Offline
        webzoid
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 ?

          1 Reply Last reply
          2

          • Login

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