Sassa
Sassa

Reputation: 3334

QGraphicsItem -> get the correct bounding rectangle

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.

Upvotes: 1

Views: 5363

Answers (3)

user1277936
user1277936

Reputation:

I would do (in 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! :)

Upvotes: 0

baysmith
baysmith

Reputation: 5202

QGraphicsItem::boundingRect() is a pure virtual function. Thus, there is no implementation. You must provide your own implementation. Based upon your vertices, probably

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

Upvotes: 2

Kaleb Pederson
Kaleb Pederson

Reputation: 46479

I'm not sure I follow, if you're using a QGraphicsItem (with or without an OpenGL viewport), you would typically use QGraphicsItem::boundingRect() to get the bounding rectangle?

Upvotes: 0

Related Questions