Issues with QMimeData.text()
Bryce Beagle
bryce.beagle at gmail.com
Sat Jun 13 01:39:40 BST 2020
Hi,
I'm trying to implement drag-and-drop behavior in my widget and I'm having
issues with the QMimeData that is provided in the QDropEvent.
When dragging text from some applications (e.g. PyCharm),
event.mimeData().text() always returns an empty string. If I manually use
event.mimeData().data('text/plain'), I get my text just fine, albeit as a
bytes object.
The Qt documentation for QMimeData::text() says that it "Returns a plain
text (MIME type text/plain) representation of the data.", so I feel that if
the 'text/plain' format is defined, this should work fine. For reference,
dragging from PyCharm, event.mimeData().formats returns:
['text/plain', 'text/plain;charset=UTF-16', 'text/plain;charset=UTF-8',
'text/plain;charset=UTF-16BE', 'text/plain;charset=UTF-16LE',
'text/plain;charset=ISO-8859-1', 'text/plain;charset=US-ASCII',
'text/plain;charset=unicode']
Here's an MCVE using PyQt 5.15.0
from PyQt5.QtGui import QDragEnterEvent, QDragMoveEvent, QDropEvent
from PyQt5.QtWidgets import QApplication, QTextEdit
class DragTextEdit(QTextEdit):
def dragEnterEvent(self, event: QDragEnterEvent):
event.acceptProposedAction()
def dragMoveEvent(self, event: QDragMoveEvent):
event.acceptProposedAction()
def dropEvent(self, event: QDropEvent):
data = event.mimeData()
print("data.text():", data.text()) # Empty string
print("data.data('text/plain'):", data.data('text/plain')) # bytes
app = QApplication([])
text_edit = DragTextEdit()
text_edit.show()
app.exec_()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20200612/47755cc1/attachment.htm>
More information about the PyQt
mailing list