mouseMoveEvent 和 mousePressEvent 中的鼠标位置不同:QGraphicsObject

Mouse position in mouseMoveEvent and mousePressEvent are different: QGraphicsObject

在以下示例中,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,但这些仅在 QGraphicsObject.

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")
        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并且演员是 QGraphicsItemQGraphicsObject。这些元素中的每一个都有不同的坐标系。

QGraphicsView 的情况下,您的 QMouseEvent returns 坐标以像素为单位,如果要将其转换为场景坐标,则必须使用 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_())

如果您想了解更多信息,请查看以下链接: