为什么椭圆位与我的鼠标位置不同?

发布于 2025-02-03 16:28:31 字数 984 浏览 2 评论 0 原文

我在这里有一个代码,当鼠标单击时,它将添加椭圆和行。

class Viewer(QtWidgets.QGraphicsView):

    def __init__(self, parent):
        super(leftImagePhotoViewer, self).__init__(parent)
        self._zoom = 0
        self._empty = True
        self._scene = QtWidgets.QGraphicsScene(self)
        self.setGeometry(QtCore.QRect(20, 90, 451, 421))
        self.setSceneRect(20, 90, 451, 421)

当我单击该行时,我有一个慕斯酶事件

   def mouseReleaseEvent(self,event): 
        pos = self.mapToScene(event.pos())
        point = self._scene.addEllipse(self._size/2, self._size/2, 10, 10, QPen(Qt.black), QBrush(Qt.green))
        point.setPos(QPointF(pos.x(),pos.y()))
        self._scene.addLine(pos.x(),pos.y(), self.posprev.x(), self.posprev.y(), QPen(Qt.green))

,其位置类似于鼠标的位置,但是椭圆上的位置对确切的鼠标位置几乎没有差距或差异。椭圆的中心应为行的端量或位置。鼠标位置是。

请参阅此处的图像:

“

有人可以帮助我有什么问题,为什么椭圆形不会添加到鼠标的确切位置?

I have a code here that will add an ellipse and line when mouse is clicked.

class Viewer(QtWidgets.QGraphicsView):

    def __init__(self, parent):
        super(leftImagePhotoViewer, self).__init__(parent)
        self._zoom = 0
        self._empty = True
        self._scene = QtWidgets.QGraphicsScene(self)
        self.setGeometry(QtCore.QRect(20, 90, 451, 421))
        self.setSceneRect(20, 90, 451, 421)

I have an MouseRelase Event

   def mouseReleaseEvent(self,event): 
        pos = self.mapToScene(event.pos())
        point = self._scene.addEllipse(self._size/2, self._size/2, 10, 10, QPen(Qt.black), QBrush(Qt.green))
        point.setPos(QPointF(pos.x(),pos.y()))
        self._scene.addLine(pos.x(),pos.y(), self.posprev.x(), self.posprev.y(), QPen(Qt.green))

When I clicked the line, its position is similar to the mouse positon, but the ellipse positon has few gap or difference to the exact mouse position.The center of the ellipse should be the endpoitn of the line or where the mouse position is.

See image here:

here

Can someone help me what is wrong why the ellipse will not add on the exact position to the mouse?

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

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

发布评论

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

评论(1

傲世九天 2025-02-10 16:28:31

作为 addellipse() ::

请注意,项目的几何形状是在项目坐标中提供的,其位置初始化为(0,0)。

这实际上对于添加基本形状的所有QGraphicsScene函数实际上是有效的,并且对于所有QGraphicSitems总体而言,初始化的位置始终为(0,0)

请考虑以下内容:

point = scene.addEllipse(5, 5, 10, 10)

以上将创建一个以(5,5) 相对于其位置的矩形中的椭圆。由于我们尚未移动它,因此该位置是场景的起源点。

椭圆形创建后立即将矩形显示为其边界的引用。

然后,我们设置了其位置(假设鼠标在 20,20 场景):

point.setPos(QPointF(20, 20))

结果将是一个椭圆形的椭圆,该矩形的左上角位于(25,25),这是相对于矩形的位置项目位置:(5,5) +(20,20)

请注意,上面显示 在原始位置中的椭圆形, setpos()

考虑到上面的情况,以下情况将正确显示椭圆中心 (20,20)

point = scene.addEllipse(-5, -5, 10, 10)
point.setPos(QPointF(20, 20))

  • ​href =“ https://doc.qt.io/qt-5/qgraphicsview.html#maptoscene” rel =“ nofollow noreferrer”> maptoscene(maptoscene() 执行 setPos(qpointf(pos.x(),pos.y())))毫无意义。
  • 请记住上面说的:所有项目在(0,0)上具有起始位置;这也适用于您在此之后创建的行,这将在 pos self.posprev 之间绘制,但是将 stall 在场景坐标中处于(0,0)
  • 视图和场景可能需要鼠标事件,特别是如果您要添加移动项目;您应该总是始终调用基本实现(在您的情况下, super()。useReleaseEvent(event))当您覆盖功能时,除非您真的知道你在做什么;
  • 正如您已经建议的那样,您阅读和理解整个图形文档,尤其是其 /graphicsview.html#The-Graphics-View-View-Coordication-System“ rel =“ nofollow noreferrer”>坐标系 works;图形视图框架与复杂的功能一样强大,并且不能仅通过反复试验来学习:能够使用它,需要大量耐心来理解其工作方式,通过仔细研究其每个班级的文档和<强>您将使用的所有功能;

As the documentation of addEllipse() explains:

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0).

This is actually valid for all QGraphicsScene functions that add basic shapes, and the initialized position is always (0, 0) for all QGraphicsItems in general.

Consider the following:

point = scene.addEllipse(5, 5, 10, 10)

The above will create an ellipse enclosed in a rectangle that starts at (5, 5) relative to its position. Since we've not moved it yet, that position is the origin point of the scene.

creating the ellipse

The ellipse as it as soon as it's created, with the rectangle shown as a reference of its boundaries.

Then, we set its position (assuming the mouse is at 20, 20 of the scene):

point.setPos(QPointF(20, 20))

The result will be an ellipse enclosed in a rectangle that has its top left corner at (25, 25), which is the rectangle position relative to the item position: (5, 5) + (20, 20).

the ellipse is translated

Note that the above shows both the ellipse in the original position and the result of setPos().

If you want an ellipse that will be centered on its position, you must create one with negative x and y coordinates that are half of the width and height of its rectangle.

Considering the case above, the following will properly show the ellipse centered at (20, 20):

point = scene.addEllipse(-5, -5, 10, 10)
point.setPos(QPointF(20, 20))

the correct approach

Notes:

  • as the documentation shows, mapToScene() already returns a QPointF, there's no point in doing setPos(QPointF(pos.x(), pos.y())): just do setPos(pos);
  • remember what said above: all items have a starting position at (0, 0); this is valid also for the line you're creating after that point, which will be drawn between pos and self.posprev, but will still be at (0, 0) in scene coordinates;
  • the view and the scene might need mouse events, especially if you're going to add movable items; you should always call the base implementation (in your case, super().mouseReleaseEvent(event)) when you override functions, unless you really know what you're doing;
  • as already suggested to you, it is of utmost importance that you read and understand the whole graphics view documentation, especially how its coordinate system works; the graphics view framework is as much powerful as it is complex, and cannot be learnt just by trial and error: being able to use it requires a lot of patience in understanding how it works by carefully studying the documentation of each of its classes and all functions you are going to use;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文