Hey I had been going through this tutorial for understanding drag and drop methods in PyQt4. However I am not able to understand the following points . It would be nice if somepne could make it clearer to me.
def mouseMoveEvent(self, e): //class Button
mimeData = QtCore.QMimeData()
drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(e.pos() - self.rect().topLeft())
dropAction = drag.start(QtCore.Qt.MoveAction)
def dropEvent(self, e): //class Example
position = e.pos()
self.button.move(position)
e.setDropAction(QtCore.Qt.MoveAction)
e.accept()
Why is there are a seperate self.button.move() and e.setDropAction() Doesnt self.button.move() actually move the button itself? And could someone explain what drag.setHotSpot and drag.start() do? Thanks.
That tutorial is seriously outdated.
QDrag.startis obsolete since Qt 4.3.QDrag.exec_should be used instead.As you can see from the docs for
exec, it has a return value.setDropActionindropEventdetermines this value. It doesn’t perform the move. That’s why you need aself.button.move()to do the actual moving. So, what’s the point of asetDropAction? You might need to know what kind of drag operation you did. Imagine you’re implementing drag-drop between two list widgets. If you did a move operation, that means you need to remove the item from the source widget and create one in the target. If it was a copy operation, you can leave the original and just create a copy in the target.setHotSpot/hotSpotis related to thesetPixmapof aQDrag. You can display aQPixmapas you drag the item.hotSpotdetermines the positioning of the pixmap. The pixmap will be positioned such that the cursor will be athotSpotrelative to the top-left corner of the pixmap. So, in the case of that tutorial, it is rather pointless since there is no pixmap to be shown.Here is a bit modified and updated version of that tutorial. Hopefully, I’ve included enough comments. You can move with
Right-Clickor copy withShift + Right-Click: