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. QGraphicsView using QOpenGLWidget as viewport, bad text rendering
Forum Updated to NodeBB v4.3 + New Features

QGraphicsView using QOpenGLWidget as viewport, bad text rendering

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 510 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.
  • C Offline
    C Offline
    closer_ex
    wrote on last edited by
    #1

    I'm subclassing a QScrollBar to provide 60fps smooth scrolling for my QGraphicsView which contains items with pixmaps and text, so I'm trying to take advantage of GPU rendering to reduce CPU usage.

    Here's the openglwidget constructor:

        explicit OpenGLViewport(QWidget *parent = nullptr) : QOpenGLWidget(parent)
        {
            QSurfaceFormat fmt = QSurfaceFormat::defaultFormat();
            fmt.setSamples(4);
            setFormat(fmt);
        }
    

    And in the graphicsview constructor:

        auto *gl = new OpenGLViewport(this);
        gl->setUpdateBehavior(QOpenGLWidget::NoPartialUpdate);
        setViewport(gl);
    

    paint function of the graphicsitem:

    void AlbumFrame::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        painter->save();
        painter->setRenderHint(QPainter::Antialiasing, true);
        painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
        painter->setRenderHint(QPainter::TextAntialiasing, true);
        if(!albumcover.isNull())
            painter->drawPixmap(coverp, albumcover);
        QFont font("Microsoft YaHei", 9);
        font.setBold(true);
        painter->setFont(font);
        painter->drawText(QRectF(0, 160, 160, 25), Qt::AlignVCenter | Qt::AlignLeft, display_albumname);
        font.setBold(false);
        painter->setFont(font);
        painter->drawText(QRectF(0, 185, 160, 25), Qt::AlignVCenter | Qt::AlignLeft, display_singer);
        painter->restore();
    }
    

    This is painted on openGLWidget:
    opengl.png
    And this is on the original widget viewport.
    non-opengl.png

    Obviously the text is painted in weird shape. Moreover, I don't see any decrease in CPU usage when scrolling. Since I don't know much about openGL, I may have missed some basic part... Any help will be appreciated!

    Qt version is 6.0.2, compiler MSVC2019 64bit.

    A 1 Reply Last reply
    0
    • C closer_ex

      I'm subclassing a QScrollBar to provide 60fps smooth scrolling for my QGraphicsView which contains items with pixmaps and text, so I'm trying to take advantage of GPU rendering to reduce CPU usage.

      Here's the openglwidget constructor:

          explicit OpenGLViewport(QWidget *parent = nullptr) : QOpenGLWidget(parent)
          {
              QSurfaceFormat fmt = QSurfaceFormat::defaultFormat();
              fmt.setSamples(4);
              setFormat(fmt);
          }
      

      And in the graphicsview constructor:

          auto *gl = new OpenGLViewport(this);
          gl->setUpdateBehavior(QOpenGLWidget::NoPartialUpdate);
          setViewport(gl);
      

      paint function of the graphicsitem:

      void AlbumFrame::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
      {
          painter->save();
          painter->setRenderHint(QPainter::Antialiasing, true);
          painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
          painter->setRenderHint(QPainter::TextAntialiasing, true);
          if(!albumcover.isNull())
              painter->drawPixmap(coverp, albumcover);
          QFont font("Microsoft YaHei", 9);
          font.setBold(true);
          painter->setFont(font);
          painter->drawText(QRectF(0, 160, 160, 25), Qt::AlignVCenter | Qt::AlignLeft, display_albumname);
          font.setBold(false);
          painter->setFont(font);
          painter->drawText(QRectF(0, 185, 160, 25), Qt::AlignVCenter | Qt::AlignLeft, display_singer);
          painter->restore();
      }
      

      This is painted on openGLWidget:
      opengl.png
      And this is on the original widget viewport.
      non-opengl.png

      Obviously the text is painted in weird shape. Moreover, I don't see any decrease in CPU usage when scrolling. Since I don't know much about openGL, I may have missed some basic part... Any help will be appreciated!

      Qt version is 6.0.2, compiler MSVC2019 64bit.

      A Offline
      A Offline
      Asperamanca
      wrote on last edited by Asperamanca
      #2

      @closer_ex
      OpenGL support in GraphicsView never really got anywhere. They realized the architecture of GraphicsView isn't a good match for hardware accelerated rendering, and created SceneGraph and QML.

      In my experience, using OpenGL with GraphicsView produces inconsistent results with painting artifacts, and not that much of a performance gain. Actually, for a CPU-only engine, plain GraphicsView is pretty fast.

      C 1 Reply Last reply
      1
      • A Asperamanca

        @closer_ex
        OpenGL support in GraphicsView never really got anywhere. They realized the architecture of GraphicsView isn't a good match for hardware accelerated rendering, and created SceneGraph and QML.

        In my experience, using OpenGL with GraphicsView produces inconsistent results with painting artifacts, and not that much of a performance gain. Actually, for a CPU-only engine, plain GraphicsView is pretty fast.

        C Offline
        C Offline
        closer_ex
        wrote on last edited by
        #3

        @Asperamanca Thanks for the reply! That explains why there is so little info about this combination on google.

        And yes the CPU rendering engine is fast, it was the debug build that should take responsibility for high CPU usage( and so much higher than I thought it would), the release build only consumes about 3% CPU resources while the debug one blows up a core, causing ui thread to stutter.

        A 1 Reply Last reply
        0
        • C closer_ex

          @Asperamanca Thanks for the reply! That explains why there is so little info about this combination on google.

          And yes the CPU rendering engine is fast, it was the debug build that should take responsibility for high CPU usage( and so much higher than I thought it would), the release build only consumes about 3% CPU resources while the debug one blows up a core, causing ui thread to stutter.

          A Offline
          A Offline
          Asperamanca
          wrote on last edited by Asperamanca
          #4

          @closer_ex
          They added a lot of highly expensive Q_ASSERT-statements sometime in Qt 5. That might be the cause of high CPU usage.

          EDIT: You can verify that by defining your own Q_ASSERT and Q_ASSERT_X macros before including the first Qt header file.

          1 Reply Last reply
          1

          • Login

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