I noticed that when I set my QLineEdit
to read only, this doesn't allow my widget to accept drops.
class CustomLineEdit(QtGui.QLineEdit):
def __init__(self):
super(CustomLineEdit, self).__init__()
self.setReadOnly(True)
self.setAcceptDrops(True)
def dropEvent(self, event):
input_text = event.mimeData().text()
if input_text.endswith('Stalk'):
self.setText(input_text.split(' ')[0])
The dragEnterEvent method that allows you to enable the dropEvent which in the case of QLineEdit by default does not accept the event when the QLineEdit is readOnly. The solution is to override that method and accept the event.
class CustomLineEdit(QtGui.QLineEdit):
def __init__(self):
super(CustomLineEdit, self).__init__()
self.setReadOnly(True)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
event.acceptProposedAction()
def dropEvent(self, event):
input_text = event.mimeData().text()
if input_text.endswith("Stalk"):
values = input_text.split(" ")
if values:
self.setText(values[0])
For more information check Drag-and-drop documentation.