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

时间:2018-05-02 06:04:17

标签: python pyqt pyqt5 qgraphicsscene qgraphicsitem

以下示例中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_())

1 个答案:

答案 0 :(得分:2)

问题是由于您要将QGraphicsView个事件发送给QGraphicsObject而导致的。在QGraphicsView的情况下,事件属于QMouseEvent类型,但在QGraphicsObject的情况下,属于QGraphicsSceneMouseEvent类型。总之,您不应将QGraphicsView事件传递给QGraphicsObject,因为它们引用了具有不同信息的不同事件。

默认情况下会启用mousePressEvent事件,但mouseMoveEvent事件无法由QGraphicsObject处理,您必须使用hoverMoveEvent但这些将会只能在boundingRect的{​​{1}}内工作。

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和参与者是QGraphicsSceneQGraphicsItem。每个元素都有不同的坐标系。

如果QGraphicsObject QGraphicsView以像素为单位返回坐标,如果要将其转换为场景坐标,则必须使用QMouseEvent

如果mapToScene() / QGraphicsItem与场景的坐标具有不同的坐标,那么这些坐标不会受到缩放,旋转等变换的影响。这就是在上一个例子。如果要将其转换为场景单位,则必须使用QGraphicsObject

在以下示例中,我以场景为单位显示所有展示次数。

mapToScene()

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

相关问题