Using PySide, I create a drag-and-drop shortcut that works exactly the way I want:
class DraggableLabel(QtGui.QLabel): def __init__(self, txt, parent): QtGui.QLabel.__init__(self, txt, parent) self.setStyleSheet("QLabel { background-color: rgb(255, 255, 0)}") def mouseMoveEvent(self, event): drag=QtGui.QDrag(self) dragMimeData=QtCore.QMimeData() drag.setMimeData(dragMimeData) drag.exec_(QtCore.Qt.MoveAction)
(Note that a complete example that uses a DraggableLabel is inserted below). Unfortunately, I do not understand what is happening with QMimeData , and I'm afraid that I will run into big problems when I use the same code in real examples.
In particular, I am concerned that my reimplementation of mouseMoveEvent creates an instance of QMimeData without the argument passed: QtCore.QMimeData() . This is normal? In more complex widgets, I will be fine if I continue to do this in the appropriate event handler: will the program automatically create the correct type of MIME data for drag and drop?
The reason I am afraid that I am missing something is because in the Qt drag and drop documentation , it has lines of code like:
mimeData -> setText(commentEdit->toPlainText());
which apparently doesnβt seem to like it, just letting the program take care of things as part of the redefinition of the event handler.
In addition, the QMimeData Documentation discusses convenient features for testing, retrieving, and setting data, but they are for standard data types (e.g. text, URLs). I did not find a clear way to define such convenient functions for widgets like my draggable QLabel . Am I missing this? Is there an easy way to find out if I am dragging a widget like X?
Edit: I tried the same code with much more complex widgets than QLabels, and it does not work.
Potentially relevant posts:
Drag and Drop QWidget in QT 5
How to drag custom widgets?
https://stackoverflow.com/questions/18272650/fill-the-system-clipboard-with-data-of-custom-mime-type Python object in QMimeData
An important warning: if you just want to move the widget out of the window, you donβt need to call esoteric drag mechanisms, but more handling vanilla events. See this: Drag / Drop QPushButton in PyQt .
The full working code of the standalone code, which includes the following:
# -*- coding: utf-8 -*- from PySide import QtGui, QtCore class LabelDrag(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.initUI() def initUI(self): self.lbl=DraggableLabel("Drag me", self) self.setAcceptDrops(True) self.setGeometry(40,50,200,200) self.show() def dragEnterEvent(self,event): event.accept() def dropEvent(self, event): self.lbl.move(event.pos()) #moves label to position once the movement finishes (dropped) event.accept() class DraggableLabel(QtGui.QLabel): def __init__(self, txt, parent): QtGui.QLabel.__init__(self, txt, parent) self.setStyleSheet("QLabel { background-color: rgb(255, 255, 0)}") def mouseMoveEvent(self, event): drag=QtGui.QDrag(self) dragMimeData=QtCore.QMimeData() drag.setMimeData(dragMimeData) drag.exec_(QtCore.Qt.MoveAction) def main(): import sys qt_app=QtGui.QApplication(sys.argv) myMover=LabelDrag() sys.exit(qt_app.exec_()) if __name__=="__main__": main()
Note. I post this on QtCentre , post something useful from there.