在以下示例中,mouseMoveEvent和mousePressEvent中的鼠标位置不同。这是由于增加了缩放比例而发生的。没有缩放比例的位置是相同的。
是否必须根据更改的缩放比例更新boundingRect?怎么样?
#!/usr/bin/env python
from PyQt5.QtCore import (QRectF)
from PyQt5.QtGui import (QPainter, QPixmap)
from PyQt5.QtWidgets import (QMainWindow, QApplication, QGraphicsObject, QGraphicsView, QGraphicsScene)
class TicTacToe(QGraphicsObject):
def __init__(self, helper):
super(TicTacToe, self).__init__()
self.mypixmap = QPixmap("exit1.png")
def paint(self, painter, option, widget):
painter.setOpacity(1)
painter.drawPixmap(0,0, 512, 512, self.mypixmap)
painter.drawLine(2,2,20,20)
def boundingRect(self):
return QRectF(0,0,512, 512)
def keyPressEvent(self, event):
print "aaaaaaaaaa"
def mouseMoveEvent(self, event):
print "ccccccccccc ", event.pos()
def mousePressEvent(self, event):
print "bbbbbbbbbbbb", event.pos()
class MyGraphicsView(QGraphicsView):
def __init__(self):
super(MyGraphicsView, self).__init__()
self.scene = QGraphicsScene(self)
self.tic_tac_toe = TicTacToe(self)
self.myScale = 2
self.tic_tac_toe.setScale(self.myScale)
self.setScene(self.scene)
self.scene.addItem(self.tic_tac_toe)
self.setMouseTracking(True)
def keyPressEvent(self, event):
self.tic_tac_toe.keyPressEvent(event)
def mouseMoveEvent(self, event):
print "mouse"
self.tic_tac_toe.mouseMoveEvent(event)
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.y = MyGraphicsView()
self.setCentralWidget(self.y)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Example()
w.show()
sys.exit(app.exec_())
最佳答案
造成此问题的原因是您将QGraphicsView
事件发送到QGraphicsObject
。对于QGraphicsView
,事件是QMouseEvent
类型,但是对于QGraphicsObject
,它是QGraphicsSceneMouseEvent
类型。总之,您不应将QGraphicsView
事件传递给QGraphicsObject
,因为它们引用具有不同信息的不同事件。mousePressEvent
事件默认情况下处于启用状态,但在mouseMoveEvent
事件的情况下,QGraphicsObject
无法处理该事件,相反,您必须使用hoverMoveEvent
,但这些仅在boundingRect
的QGraphicsObject
内部起作用>。
#!/usr/bin/env python
from PyQt5.QtCore import (QRectF)
from PyQt5.QtGui import (QPainter, QPixmap)
from PyQt5.QtWidgets import (QMainWindow, QApplication, QGraphicsObject, QGraphicsView, QGraphicsScene)
class TicTacToe(QGraphicsObject):
def __init__(self, helper):
super(TicTacToe, self).__init__()
self.mypixmap = QPixmap("exit1.png")
self.setAcceptHoverEvents(True)
def paint(self, painter, option, widget):
painter.setOpacity(1)
painter.drawPixmap(0,0, 512, 512, self.mypixmap)
painter.drawLine(2,2,20,20)
def boundingRect(self):
return QRectF(0,0,512, 512)
def hoverMoveEvent(self, event):
print("ccccccccccc ", event.pos())
def mousePressEvent(self, event):
print("bbbbbbbbbbbb", event.pos())
class MyGraphicsView(QGraphicsView):
def __init__(self):
super(MyGraphicsView, self).__init__()
self.scene = QGraphicsScene(self)
self.tic_tac_toe = TicTacToe(self)
self.myScale = 2
self.tic_tac_toe.setScale(self.myScale)
self.setScene(self.scene)
self.scene.addItem(self.tic_tac_toe)
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.y = MyGraphicsView()
self.setCentralWidget(self.y)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Example()
w.show()
sys.exit(app.exec_())
另一方面,这些点与场景中的位置不一致,因为这些坐标是相对于项目的。
为了让您更好地理解,我们可以使用以下类比,假设您正在使用摄像机录制场景,摄像机的屏幕就像
QGraphicsView
,场景是QGraphicsScene
,演员是QGraphicsItem
, QGraphicsObject
s。这些元素中的每一个都有不同的坐标系。对于
QGraphicsView
,您的QMouseEvent
以像素为单位返回坐标,如果要将其转换为场景的坐标,则必须使用mapToScene()
。在
QGraphicsItem
/ QGraphicsObject
与场景的坐标不同的情况下,那些坐标不受缩放,旋转等转换的影响。这就是前面示例中的打印内容。如果要将其转换为场景单位,则必须使用mapToScene()
。在以下示例中,我以场景为单位显示所有印象。
#!/usr/bin/env python
from PyQt5.QtCore import (QRectF)
from PyQt5.QtGui import (QPainter, QPixmap)
from PyQt5.QtWidgets import (QMainWindow, QApplication, QGraphicsObject, QGraphicsView, QGraphicsScene)
class TicTacToe(QGraphicsObject):
def __init__(self, helper):
super(TicTacToe, self).__init__()
self.mypixmap = QPixmap("exit1.png")
self.setAcceptHoverEvents(True)
def paint(self, painter, option, widget):
painter.setOpacity(1)
painter.drawPixmap(0,0, 512, 512, self.mypixmap)
painter.drawLine(2,2,20,20)
def boundingRect(self):
return QRectF(0,0,512, 512)
def hoverMoveEvent(self, event):
#print("hoverMoveEvent ", event.pos())
print("hoverMoveEvent", self.mapToScene(event.pos()))
def mousePressEvent(self, event):
#print("mousePressEvent", event.pos())
print("mousePressEvent", self.mapToScene(event.pos()))
class MyGraphicsView(QGraphicsView):
def __init__(self):
super(MyGraphicsView, self).__init__()
self.scene = QGraphicsScene(self)
self.setMouseTracking(True)
self.tic_tac_toe = TicTacToe(self)
self.myScale = 2
self.tic_tac_toe.setScale(self.myScale)
self.setScene(self.scene)
self.scene.addItem(self.tic_tac_toe)
def mouseMoveEvent(self, event):
print("mouseMoveEvent", self.mapToScene(event.pos()))
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.y = MyGraphicsView()
self.setCentralWidget(self.y)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Example()
w.show()
sys.exit(app.exec_())
如果您想了解更多信息,请查看以下链接:
http://doc.qt.io/qt-5/graphicsview.html
http://blog.qt.io/blog/2017/01/19/should-you-be-using-qgraphicsview/