<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">Le 13/06/20 à 02:39, Bryce Beagle a
      écrit :<br>
    </div>
    <blockquote type="cite"
cite="mid:CAKKt4mZQ5rEGVQyt4z4=6U1j2OnzjmcCmjsaSwixxsi5Mh1q-g@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div dir="ltr">Hi,
        <div><br>
        </div>
        <div>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. </div>
        <div><br>
        </div>
        <div>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. </div>
        <div><br>
        </div>
        <div>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:</div>
        <div><br>
        </div>
        <div>['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']<br>
        </div>
        <div><br>
        </div>
        <div>Here's an MCVE using PyQt 5.15.0</div>
        <div><br>
        </div>
        <div><font face="monospace">from PyQt5.QtGui import
            QDragEnterEvent, QDragMoveEvent, QDropEvent<br>
            from PyQt5.QtWidgets import QApplication, QTextEdit<br>
            <br>
            <br>
            class DragTextEdit(QTextEdit):<br>
            <br>
                def dragEnterEvent(self, event: QDragEnterEvent):<br>
                    event.acceptProposedAction()<br>
            <br>
                def dragMoveEvent(self, event: QDragMoveEvent):<br>
                    event.acceptProposedAction()<br>
            <br>
                def dropEvent(self, event: QDropEvent):<br>
                    data = event.mimeData()<br>
                    print("data.text():", data.text())  # Empty string<br>
                    print("data.data('text/plain'):",
            data.data('text/plain'))  # bytes<br>
            <br>
            <br>
            app = QApplication([])<br>
            text_edit = DragTextEdit()<br>
            text_edit.show()<br>
            app.exec_()</font><br>
        </div>
      </div>
    </blockquote>
    <p>Hi,</p>
    <p>I've changed your code: <br>
    </p>
    <p>    def dropEvent(self, event: QDropEvent):<br>
              data = event.mimeData()<br>
              print("data.text():", data.text(), type(data.text()))<br>
              print("data.data('text/plain'):",
      str(data.data('text/plain')),<br>
                     type(data.data('text/plain')))<br>
    </p>
    <p>And the result:</p>
    data.text(): Déjà-vu <class 'str'><br>
    data.data('text/plain'): b'D\xc3\xa9j\xc3\xa0-vu' <class
    'PyQt5.QtCore.QByteArray'><br>
    <p>On Ubuntu Mate, the text "Déjà-vu" is dragged from Pluma (old
      Gedit)</p>
    <p>Vincent<br>
    </p>
  </body>
</html>