QGraphicsItem doesn't move when mouseMoveEvent in QGraphicsView is overloaded

30 Views Asked by At

Here is my code:

import sys

from PyQt6.QtCore import Qt, QPoint
from PyQt6.QtWidgets import QGraphicsItem, QWidget, QApplication, QMainWindow, QVBoxLayout, QGraphicsScene, QGraphicsView, QGraphicsItem, QGraphicsRectItem
from PyQt6.QtGui import QMouseEvent

        
class CustomRect(QGraphicsRectItem):
    
    def __init__(self, parent: 'CustomView'):
        super().__init__()
        self.show()
        self.scene_of_view = parent.scene()
        self.scene_of_view.addItem(self)
        self.setRect(0, 0, 400, 400)
        self.setFlag(QGraphicsItem.GraphicsItemFlag.ItemIsMovable, True)
        self.setFlag(QGraphicsItem.GraphicsItemFlag.ItemSendsGeometryChanges, True)
        
        
class CustomView(QGraphicsView):
    
    def __init__(self, parent):
        super().__init__(QGraphicsScene(), parent)
        self.show()
        self.setSceneRect(0, 0, 2000, 2000)
        self.is_translation = False
        self.last_translation_point: QPoint
        self.setTransformationAnchor(QGraphicsView.ViewportAnchor.NoAnchor)
        
    def mousePressEvent(self, event: QMouseEvent):
        if event.buttons() == Qt.MouseButton.MiddleButton:
            self.last_translation_point = event.pos()
            self.is_translation = True
        return super().mousePressEvent(event)
    
    def mouseMoveEvent(self, event: QMouseEvent):
        if event.buttons() == Qt.MouseButton.MiddleButton and self.is_translation:
            new_point = event.pos()
            translation = new_point.toPointF() - self.last_translation_point.toPointF()
            self.translate(translation.x(), translation.y())
            self.last_translation_point = new_point
        return super().mousePressEvent(event)
    
    def mouseReleaseEvent(self, event: QMouseEvent):
        if event.buttons() == Qt.MouseButton.MiddleButton:
            self.is_translation == False
        return super().mousePressEvent(event)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = QMainWindow()
    main_window.setGeometry(0, 0, 1500, 700)
    main_window.setCentralWidget(QWidget())
    main_window.centralWidget().setLayout(QVBoxLayout())
    
    view = CustomView(main_window)
    main_window.centralWidget().layout().addWidget(view)
    
    rect = CustomRect(view)
    rect.setPos(100, 100)
    
    rect2 = CustomRect(view)
    rect2.setPos(600, 100)
    
    main_window.show()
    app.exec()

I try make movement in scene, but I need items moving also (by mouse).
Now, movement in scene works well, but items moving doesn't works at all.
When I remove mouseMoveEvent method from CustomView items moving enables, but obviously movement in scene disable.
I tried use event.accept() and event.ignore() in CustomRect.mousePressEvent, CustomView.mousePressEvent, CustomView.mouseMoveEvent, but nothing changes.
It's seems like CustomRect.mouseMoveEvent doesn't firing.
I also noticed when I move second rect after moving first rect, a mouse grab first rect instead of second, and it move to cursor.

0

There are 0 best solutions below