Drag and Drop Inside ImageView

61 views
Skip to first unread message

mserlin

unread,
Jan 18, 2017, 9:54:53 PM1/18/17
to pyqtgraph
I've added shapes to an ImageView environment that I would like to drag around. 

To do this, I've created a subclass of the Graphics object which overrides the mousePressEvent and mouseMoveEvent. This are then added to my view (at pyqtgraph PlotItem), which I'm also using as the view for the ImageView. An example is shown below. 

class dragLine(QtGui.QGraphicsLineItem):
    
    def __init__(self,X1, Y1, X2, Y2):
        super(dragLine,self).__init__(X1,Y1,X2,Y2)
        self.setAcceptDrops(True)
        
    def mousePressEvent(self,e):
        #This works fine
        super(dragLine,self).mousePressEvent(e)
        
        if e.button() == QtCore.Qt.LeftButton:
            print 'Pressed!'
        
        self.grabMouse()
  
    def mouseMoveEvent(self, e):
        #This never gets called
        if e.buttons() != QtCore.Qt.LeftButton:
            return

        mimeData = QtCore.QMimeData()

        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        drag.setHotSpot(e.pos())
        
        dropAction = drag.start(QtCore.Qt.MoveAction)

Next, from what I've read, I need to set setAcceptsDrop to true, and to override dragEnterEvent, dragMoveEvent, and/or dropEvent. I'm unclear about which object I need to do this for. I've tried doing for the window in which the ImageView is being placed, the PlotItem which the ImageView is using as its view, and for the ImageView item itself. The code below is an example of what I have so far. 

class plot2D(pg.ImageView):
    def __init__(self,prnt = None, viw = None):
        super(plot2D,self).__init__(parent = prnt, view = viw) 
        self.setAcceptDrops(True)
        
    def dragEnterEvent(self, e):
        print 'Accept drag event.'
        e.accept()
        
    def dragMoveEvent(self, e):
        print 'Accept dragMove event.'
        e.accept()

    def dropEvent(self, e):
        print 'Drop event'
        e.accept()  

For everything I've tried so far, mousePressEvents are registering, but won't they won't grab the mouse. Instead it will grab the image in the behind the objects I've placed and move around the entire image area. Given that the mousePressEvent works fine, I'm guessing I'm doing something wrong when it comes to where I should be overriding the drag events and calling setAcceptsDrop(True) (which I have started to add everywhere conceivable just for good measure). Does anyone have any idea what I could try to make this work?

Thanks!


Reply all
Reply to author
Forward
0 new messages