halflings
halflings

Reputation: 1538

Convert QmouseEvent to QGraphicsSceneMouseEvent for a drag movement

I want to move QGraphicsScene instead of moving my whole widget (which caused some graphical glitches due to the widget moving off-limit).

This is what I tried to do :

def mousePressEvent(self, event):
    #super(SvgView, self).mousePressEvent(event)

    self.svgItem.mousePressEvent(event)

    self.x += event.pos().x()
    self.y += event.pos().y()
    event.accept()

(notice that I'm not updating my self.x and self.y the right way, as this is a "drag" movement and I'm supposed to put the "delta" instead, but I don't know how to get it)

This doesn't work because svgItem expects a QGraphicsSceneMouseEvent.

I tried doing this mouseEvent on the scene instead of doing it on the QGraphicsView, but it doesn't make the item drag.

Is there anyway to do this without doing this conversion, and if it's necessary how can I do it?

Upvotes: 1

Views: 1905

Answers (1)

Alex
Alex

Reputation: 3257

For me SVG + Javascript is very understandable and with it I can do whatever I want. But I am struggling with PyQt for desktop app. Avaris helps me to understand something about event propagation in Qt. I wrote few lines for experimenting, but maybe it may help you. Not the best, but "eppur si muove":

from PySide import QtGui, QtCore

class View(QtGui.QGraphicsView): # as separate class
    def BlahBlah():
        print dir()

class Scene_w_ellipse(QtGui.QGraphicsScene):
    def __init__(self):
        super(Scene_w_ellipse, self).__init__()
        self.setSceneRect(0,0,300,200)
        self.C = QtGui.QGraphicsEllipseItem(-4,-20,80,40)
        self.C.setPos(100,70)
        self.C.setFlags(QtGui.QGraphicsItem.ItemIsMovable)
        #self.C.mousePressEvent(QtGui.QGraphicsSceneMouseEvent)
        self.addItem(self.C)

    def mousePressEvent(self, event):
        print dir()

    def mouseMoveEvent(self, event):
        self.C.mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        self.C.mouseReleaseEvent(event)

For your background, don't subclass, build it as separate class.

Upvotes: 1

Related Questions