QT 在 QGraphicsView 中使用 opengl 不能够刷新的解决方案

admin 2022年6月18日23:39:35评论83 views字数 1660阅读5分32秒阅读模式

症状

在QGraphicsView的事件中,不论使用 update,repaint,抑或updateScence,resetCacheContent, 均不可以刷新界面

程序里参考上一篇博文的方法,在QGraphicsView中使用了Opengl,即,把QGraphicsView的视口委托给QGLWidget来渲染

参考资料

一个比一个坑爹,都不管用

http://lists.trolltech.com/qt-interest/2007-02/thread00033-0.html

http://www.qtforum.org/article/19433/repainting-a-qgraphicsview-after-the-scene-is-modified.html

http://www.qtcentre.org/threads/12089-GraphicsView-update-background

解决方案

全球首发,过了这村就没这店

尼玛要调用 viewport 的update函数!!!

参考代码

首先,把QGLWidget绑定到QGraphicsView上,从而可以使用opengl进行渲染

void MYGraphicsView::setRenderGLWidget(QGLWidget *widget){    this->setViewport(widget);    this->setViewportUpdateMode(         QGraphicsView::FullViewportUpdate);}

在 drawBackground 函数中使用opengl画图


void MYGraphicsView::drawBackground(QPainter *,const QRectF &){    glClearColor(1,1,1,1);    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     glMatrixMode(GL_PROJECTION);    glPushMatrix();    glLoadIdentity();    gluOrtho2D(0,1,0,1);     glMatrixMode(GL_MODELVIEW);    glPushMatrix();    glLoadIdentity();     glColor3f(0,0,0);    glLineWidth(2);     float margin=0.05;    float l=margin,r=1-margin,b=margin,t=1-margin;    int splitNum=9;    float dx=(1-margin*2)/(splitNum+1);     glBegin(GL_LINE_LOOP);        glVertex2f(l,b);        glVertex2f(l,t);        glVertex2f(r,t);        glVertex2f(r,b);    glEnd();     glBegin(GL_LINES);    for(int i=1;i<=splitNum;++i)    {        glVertex2f(l,b+dx*i);        glVertex2f(r,b+dx*i);        glVertex2f(l+dx*i,b);        glVertex2f(l+dx*i,t);    }    glEnd();    glPopMatrix();     glMatrixMode(GL_PROJECTION);    glPopMatrix();}

  最后,尼玛坑爹的事件更新

  


void MYGraphicsView::wheelEvent( QWheelEvent * event ){    double factor=event->delta()>0?1.1:1/1.1;    mpiMsg.scale.x*=factor;    mpiMsg.scale.y=mpiMsg.scale.x;  mpiMsg.broadCast(M*N);    MPI_Barrier(MPI_COMM_WORLD);}

this->viewport()->update();

QT 在 QGraphicsView 中使用 opengl 不能够刷新的解决方案


原文始发于微信公众号(汇编语言):QT 在 QGraphicsView 中使用 opengl 不能够刷新的解决方案

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年6月18日23:39:35
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   QT 在 QGraphicsView 中使用 opengl 不能够刷新的解决方案https://cn-sec.com/archives/1126882.html

发表评论

匿名网友 填写信息