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;

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

	mpiMsg.broadCast(M*N);
	MPI_Barrier(MPI_COMM_WORLD);
}

  

 

posted on 2011-11-14 00:25  大宝pku  阅读(8152)  评论(2编辑  收藏  举报

导航