问题描述
|
我正在尝试OpenGL ES 2.0的问候三角形示例。我正在使用Qt,所以我创建了QGraphicsScene并将该代码添加为QGraphicsItem。它正确绘制,但是我无法正确获得边界矩形。三角形顶点是
GLfloat afVertices[] =
{-0.4f,-0.4f,0.0f,0.4f,0.0f};
我的视口是glViewport(0,800,480);
正确的边界矩形坐标是什么?
我将视口设置为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)
这样的值时,这个看似正确的值被创建了-太“手动”了,所以我想知道是否存在某种我不知道的坐标对应或变换。
解决方法
ѭ5是纯虚函数。因此,没有实现。您必须提供自己的实现。根据您的顶点,可能
QRectF myGraphicsItem::boundingRect() const
{
return QRectF(-0.4,-0.4,0.8,0.8);
}
, 我不确定我是否遵循,如果您使用的是QGraphicsItem(带有或不带有OpenGL视口),通常会使用QGraphicsItem :: boundingRect()来获取边界矩形吗?
, 我会做(在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! :)