QGraphicsItem ->;得到正确的边界矩形

发布于 2024-11-15 14:15:46 字数 823 浏览 3 评论 0 原文

我正在尝试 OpenGL ES 2.0 的 hello 三角形示例。我正在使用 Qt,因此我创建了一个 QGraphicsScene 并将该代码添加为 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);

正确的边界矩形坐标是什么?

我将视口设置为 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) 这样的值时创建了看似正确的值 - 这太“手动” - 所以我想知道也许有一些我不知道的一种坐标对应或变换。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太阳男子 2024-11-22 14:15:46

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

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

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);
}
空心空情空意 2024-11-22 14:15:46

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

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?

春花秋月 2024-11-22 14:15:46

我会这样做(用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! :)

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! :)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文