Using PySide/PyQt, I construct a draggable label that works exactly how 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.setMimeData(dragLabMimeData)
drag.exec_(QtCore.Qt.MoveAction)
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)
dragLabMimeData=QtCore.QMimeData()
drag.setMimeData(dragLabMimeData)
drag.exec_(QtCore.Qt.MoveAction)
To copy to clipboard, switch view to plain text mode
Unfortunately, I do not understand what is going on with QMimeData, and fear I am going to run into big problems when I use similar code in real-world examples.
In particular, I am worried that my reimplementation of mouseMoveEvent creates an instance of QMimeData without any argument passed: QtCore.QMimeData(). Is this normal? Within more complex widgets will I be OK if I keep doing that within the relevant event handler: will the program automatically create the right type of MIME data for dragging and dropping?
The reason I fear I am missing something is because at the Qt Drag and Drop documentation, it has lines of code like:
mimeData -> setText(commentEdit->toPlainText());
mimeData -> setText(commentEdit->toPlainText());
To copy to clipboard, switch view to plain text mode
which seems decidedly not like just letting the program take care of things within a reimplementation of an event handler.
Also, the QMimeData documentation discusses convenience functions to test, get, and set data, but those are for standard data types (e.g., text, urls). I have found no clear way to define such convenience functions for widgets like my draggable QLabel. Am I missing it? Is there a simple way to find out if I am dragging around a widget of type X?
Note I posted this also at Stack Overflow:
http://stackoverflow.com/questions/2...itrary-widgets
Bookmarks