PyQt6 mouseMoveEvent fires crazy when dragging left and right

25 views Asked by At

i have a mouse move event build in PyQt6, but the problem is, that when I move my rectangle, it is moving spontaneously from left to right.

Here you have a example of my code:

class GanttItem(QGraphicsItem):
def __init__(self, start, duration, name, color, parent=None):
    super().__init__(parent)
    self.start = start
    self.duration = duration
    self.name = name
    self.color = color
    self.dragging = False

def boundingRect(self):
    return QRectF(0, 0, self.duration * 30, 50)

def paint(self, painter, option, widget):
    painter.setBrush(QBrush(self.color))
    painter.drawRect(self.boundingRect())
    painter.drawText(self.boundingRect(), Qt.AlignmentFlag.AlignCenter, self.name)

def mousePressEvent(self, event: QMouseEvent):
    self.dragging = True
    self.mouse_press_pos = event.pos()

def mouseReleaseEvent(self, event: QMouseEvent):
    self.dragging = False

def mouseMoveEvent(self, event: QMouseEvent):
    if self.dragging:

        event_x = event.pos().x()

        print(event_x)

        self.setPos(event_x, self.pos().y())

Demo

And due to the print, I get these e.g. console outputs:

332.0
-212.0
331.0
-213.0
90.0
-210.0

so u can see that the cursor jumps randomly from 332.0 points to -212.0 points. Has anyone else experienced that? I think that the MouseEvent is fired multiple times, but i can not verify how to filter them.

0

There are 0 answers