1

I am trying the hello triangle example of OpenGL ES 2.0. I am using Qt, so I created a QGraphicsScene and added that code as a QGraphicsItem. It draws correctly, but I cannot get the bounding rectangle correctly. The triangle vertices are

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

and my viewport is glViewport(0, 0, 800, 480);

What would be the correct bounding rect coordinates?

I set the viewport to a QGLWidget. The thing with the QGraphicsItem is that I have to re-implement the bounding rectangle of the item and if I just use

QRectF myGraphicsItem::boundingRect() const
{

   return QGraphicsItem::boundingRect();
} 

it says undefined reference to `QGraphicsItem::boundingRect() const'

I had originally used

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

but the result is a very small bounding box. The seemingly correct one was created when I was used values like QRectf(300, 200, 200, 200) by trial and error -which is too 'manual'-, so I was wondering maybe there is some kind of coordinate correspondence or transformation that I'm unaware of.

4

3 回答 3

2

QGraphicsItem::boundingRect()是纯虚函数。因此,没有实现。您必须提供自己的实现。根据您的顶点,可能

QRectF myGraphicsItem::boundingRect() const
{
  return QRectF(-0.4, -0.4, 0.8, 0.8);
}
于 2011-06-15T03:18:38.873 回答
0

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

于 2011-06-14T22:51:16.003 回答
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! :)
于 2016-07-29T22:00:54.253 回答