I'm trying to implement Drag&Drop in my application, but the dropEvent in the target widget is never called.
I searched this problem a lot but every solution I found involves overriding dragMoveEvent, which I did, but with no difference.
This example code for me is not working either, for the above reason:
Main window class:
class Win(QtWidgets.QWidget):
def __init__(self):
super(Win, self).__init__()
self.setGeometry(200, 300, 400, 200)
self.setLayout(QtWidgets.QHBoxLayout())
self.layout().addWidget(DragLabel())
self.layout().addWidget(DropTest())
Label to drag:
class DragLabel(QtWidgets.QLabel):
def __init__(self):
super(DragLabel, self).__init__()
self.setText("Drag me")
def mouseMoveEvent(self, e):
if e.buttons() != QtCore.Qt.LeftButton:
return
mimeData = QtCore.QMimeData()
mimeData.setText("Test drop")
drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
dropAction = drag.exec(QtCore.Qt.CopyAction)
Widget to drop onto:
class DropTest(QtWidgets.QWidget):
def __init__(self):
super(DropTest, self).__init__()
self.setAcceptDrops(True)
def dragEnterEvent(self, e):
print("DragEnter")
e.accept()
def dragMoveEvent(self, e):
print("DragMove")
e.accept()
def dropEvent(self, e):
print("DropEvent")
position = e.pos()
print(position)
e.accept()
When I drag the label onto the other widget I see that both dragEnterEvent and dragMoveEvent are being called, but when I actually drop the label I get no message from the dropEvent function.
Plus, after closing the window the application will hang and won't quit.
I'm using PyQt 5.13.1 x86_64 installed with DNF in Fedora 31. Python version is 3.7.5 with no virtualenv.
from dropEvent not firing in PyQt5
No comments:
Post a Comment