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

Screen Tearing QOpenGLWidget for 2D Images Drawn with QPainter



  • Hi,

    I am trying to develop a QT Widgets app, by which I need to rotate large 2D images in real time. When doing so, I see some flickering/tearing of the images when they are moving quickly across the screen.

    Currently, I am using a QOpenGLWidget and drawing all the resources in the paintEvent method, and using a timer to call update() to refresh the screen

    MyWidget::MyWidget(QWidget *parent) :
        QOpenGLWidget(parent),
        background(":/images/background.png"),
        img1(":/images/resource1.png"),
        img2(":/images/resource2.png")
    {
        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(update()));
        timer->start(0);
    }
    void MyWidget::paintEvent(QPaintEvent *event)
    {
        if (m_frameCount  == 0) {
             m_time.start();
         }
         m_frameCount++;
         QPainter painter(this);
         painter.drawPixmap(0,0,background);
         painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, true);
         painter.drawPixmap(QPoint(464.0, 20.0), img1);
         QTransform trans;
         trans.translate(960,546);
         trans.rotate(240.0 / 8.0 * m_framecount / 1000.0 % 8000);
         trans.translate(-960, -546);
         painter.setTransform(trans);
         painter.drawImage(QPoint(714,450), img2);
         painter.resetTransform();
         painter.end();
    }
    

    I am getting around 30 FPS, I suppose the artifacts are from the refresh rate, is there any way to enable VSync?

    I am only drawing many 2D images with a lot of rotations and translations in a from a fixed viewpoint with a fixed background, is there a more efficient way to display these items utilizing OpenGL that will mitigate the artifacts?

    Is there a way to cache the background to increase performance?

    Thanks for the help!


Log in to reply