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

症状

在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();

  

 

posted on   大宝pku  阅读(8275)  评论(2编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库

导航

< 2011年11月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 1 2 3
4 5 6 7 8 9 10
点击右上角即可分享
微信分享提示