Render QImage with OpenGL

前端 未结 2 1985
[愿得一人]
[愿得一人] 2021-02-06 07:54

Related to my other question, I think the more central question would be, how you render a QImage with OpenGL?

I think the code has to look something like t

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-06 08:43

    Nowadays, with Qt 5.11, the easiest way is to use QOpenGLWidget and override its paintEvent method.

    In the following code OpenGLWidget inherite from QOpenGLWidget, and display(const QImage& img) is a slot triggered whenever I need to update the image.

    No matter how many times display() is called between two OpenGL rendering, the update() call doesn't trig rendering directly, it just marks widget dirty for redrawing on next loop.

    This method is actually pretty fast, I'm using it to display camera frames at 240fps (although not all frames are displayed on screen).

    void OpenGLWidget::display(const QImage& img)
    {
      image = img;
      this->update();
    }
    
    void OpenGLWidget::paintEvent(QPaintEvent*)
    {
      QPainter painter(this);
      painter.drawImage(this->rect(),image);
    }
    

提交回复
热议问题