为什么椭圆位与我的鼠标位置不同?
我在这里有一个代码,当鼠标单击时,它将添加椭圆和行。
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))
,其位置类似于鼠标的位置,但是椭圆上的位置对确切的鼠标位置几乎没有差距或差异。椭圆的中心应为行的端量或位置。鼠标位置是。
请参阅此处的图像:
有人可以帮助我有什么问题,为什么椭圆形不会添加到鼠标的确切位置?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为
addellipse()
::这实际上对于添加基本形状的所有QGraphicsScene函数实际上是有效的,并且对于所有QGraphicSitems总体而言,初始化的位置始终为
(0,0)
。请考虑以下内容:
以上将创建一个以
(5,5)
相对于其位置的矩形中的椭圆。由于我们尚未移动它,因此该位置是场景的起源点。椭圆形创建后立即将矩形显示为其边界的引用。
然后,我们设置了其位置(假设鼠标在
20,20
场景):结果将是一个椭圆形的椭圆,该矩形的左上角位于
(25,25)
,这是相对于矩形的位置项目位置:(5,5) +(20,20)
。请注意,上面显示 在原始位置和中的椭圆形,
setpos()
考虑到上面的情况,以下情况将正确显示椭圆中心
(20,20)
:
maptoscene(maptoscene()
执行setPos(qpointf(pos.x(),pos.y())))
毫无意义。(0,0)
上具有起始位置;这也适用于您在此之后创建的行,这将在pos
和self.posprev
之间绘制,但是将 stall 在场景坐标中处于(0,0)
;super()。useReleaseEvent(event)
)当您覆盖功能时,除非您真的知道你在做什么;As the documentation of
addEllipse()
explains: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:
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.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):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)
.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)
:Notes:
mapToScene()
already returns a QPointF, there's no point in doingsetPos(QPointF(pos.x(), pos.y()))
: just dosetPos(pos)
;(0, 0)
; this is valid also for the line you're creating after that point, which will be drawn betweenpos
andself.posprev
, but will still be at(0, 0)
in scene coordinates;super().mouseReleaseEvent(event)
) when you override functions, unless you really know what you're doing;