2011-06-14 118 views
0

我在尝试OpenGL ES 2.0的hello三角形示例。我使用Qt,所以我创建了一个QGraphicsScene并将该代码添加为QGraphicsItem。它绘制正确,但我无法正确获取边界矩形。三角形顶点是QGraphicsItem - >获取正确的边界矩形

GLfloat afVertices[] = 
{-0.4f,-0.4f,0.0f, 
0.4f ,-0.4f,0.0f, 
0.0f ,0.4f ,0.0f}; 

和我的视窗是glViewport(0, 0, 800, 480);

什么是正确的边界RECT坐标?

我将视口设置为QGLWidget。用的QGraphicsItem的事情是,我不得不重新实施该项目的边界矩形,如果我只是用

QRectF myGraphicsItem::boundingRect() const 
{ 

    return QGraphicsItem::boundingRect(); 
} 

它说:未定义的参考`的QGraphicsItem :: boundingRect()const的”

我原本使用

QRectF myGraphicsItem::boundingRect() const 
{ 
    return QRectF(-0.4, -0.4, 0.8, 0.8); 
} 

但结果是一个非常小的边界框。看似正确的一个是当我通过试验和错误使用像QRectf(300, 200, 200, 200)这样的值时,它太“手动”了,所以我想知道是否有某种我不知道的坐标对应或变换。

回答

0

我不确定我是否遵循,如果您使用的是QGraphicsItem(带或不带OpenGL视口),您通常会使用QGraphicsItem::boundingRect()来获取边界矩形?

2

QGraphicsItem::boundingRect()是一个纯虚函数。因此,没有实施。你必须提供你自己的实现。基于您的顶点,可能

QRectF myGraphicsItem::boundingRect() const 
{ 
    return QRectF(-0.4, -0.4, 0.8, 0.8); 
} 
0

我会做(在Python):

# inside class 
    def parentBoundingRect(self): 
     return self.mapToParent(self.boundingRect()).boundingRect() 

# or if that doesn't work 
    def parentBoundingRect(self): 
     pos = self.pos() 
     rect = self.transform().mapToPolygon(self.boundingRect()).boundingRect() 
     return QRectF(pos.x(), pos.y(), rect.width(), rect.height()) 

# or if that doesn't work, keep playing with it til it does! :)