Issues with QMimeData.text()
Bryce Beagle <[email protected]> Fri, 12 Jun 2020 17:39:40 -0700
| Newsgroups | gmane.comp.python.pyqt-pykde |
|---|---|
| Message-ID | <CAKKt4mZQ5rEGVQyt4z4=6U1j2OnzjmcCmjsaSwixxsi5Mh1q-g@mail.gmail.com> |
--000000000000830c2605a7ec6e6a
Content-Type: text/plain; charset="UTF-8"
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_()
--000000000000830c2605a7ec6e6a
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hi,<div><br></div><div>I'm trying to implement drag-an=
d-drop behavior in my widget and I'm having issues with the QMimeData t=
hat is provided in the QDropEvent.=C2=A0</div><div><br></div><div>When drag=
ging text from some applications (e.g. PyCharm), event.mimeData().text() al=
ways returns an empty string. If I manually use event.mimeData().data('=
text/plain'), I get my text just fine, albeit as a bytes object.=C2=A0<=
/div><div><br></div><div>The Qt documentation for QMimeData::text() says th=
at it "Returns a plain text (MIME type text/plain) representation of t=
he data.", so I feel that if the 'text/plain' format is define=
d, this should work fine. For reference, dragging from PyCharm, event.mimeD=
ata().formats returns:</div><div><br></div><div>['text/plain', '=
;text/plain;charset=3DUTF-16', 'text/plain;charset=3DUTF-8', &#=
39;text/plain;charset=3DUTF-16BE', 'text/plain;charset=3DUTF-16LE&#=
39;, 'text/plain;charset=3DISO-8859-1', 'text/plain;charset=3DU=
S-ASCII', 'text/plain;charset=3Dunicode']<br></div><div><br></d=
iv><div>Here's an MCVE using PyQt 5.15.0</div><div><br></div><div><font=
face=3D"monospace">from PyQt5.QtGui import QDragEnterEvent, QDragMoveEvent=
, QDropEvent<br>from PyQt5.QtWidgets import QApplication, QTextEdit<br><br>=
<br>class DragTextEdit(QTextEdit):<br><br>=C2=A0 =C2=A0 def dragEnterEvent(=
self, event: QDragEnterEvent):<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 event.acceptP=
roposedAction()<br><br>=C2=A0 =C2=A0 def dragMoveEvent(self, event: QDragMo=
veEvent):<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 event.acceptProposedAction()<br><b=
r>=C2=A0 =C2=A0 def dropEvent(self, event: QDropEvent):<br>=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 data =3D event.mimeData()<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 prin=
t("data.text():", data.text()) =C2=A0# Empty string<br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 print("data.data('text/plain'):", data.=
data('text/plain')) =C2=A0# bytes<br><br><br>app =3D QApplication([=
])<br>text_edit =3D DragTextEdit()<br>text_edit.show()<br>app.exec_()</font=
><br></div></div>
--000000000000830c2605a7ec6e6a--