<div dir="ltr">Hi all,<div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"></div></div></div></div></div></div></div></div></div>
<div><br></div><div>I'm trying to work around <a href="https://bugreports.qt.io/browse/QTBUG-61562">QTBUG-61562</a>: On Mac, this bug prepends invisible Unicode characters to the clipboard when copy/pasting.</div><div><br></div><div>The class <a href="https://doc.qt.io/qt-5/qmacpasteboardmime.html">QMacPasteboardMime</a> lets one customize copy/pasting on Mac. I created a subclass of it, which does get picked up by Qt. However, the crucial methods canConvert(...) and convertFromMime(...) aren't called. Only the more preliminary convertorName() and flavorFor(...) of my subclass are invoked.</div><div><br></div><div>Does anyone have experience with this? Here is my code, adapted from a solution posted in the discussion of QTBUG-61652:</div><div><br></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>from PyQt5.QtMacExtras import QMacPasteboardMime</div><div><br></div><div>class MyMime(QMacPasteboardMime):</div><div> def __init__(self):</div><div> super().__init__(QMacPasteboardMime.MIME_CLIP)</div><div> def convertorName(self):</div><div> return 'UnicodeTextUtf8Default'</div><div> def flavorFor(self, mime):</div><div> if mime == 'text/plain':</div><div> return 'public.utf8-plain-text'</div><div> i = mime.find('charset=')</div><div> if i >= 0:</div><div> charset = mime[i + len('charset='):]</div><div> charset = charset.split(';', 1)[0]</div><div> if charset == 'system':</div><div> return 'public.utf8-plain-text'</div><div> if charset in ('iso-10646-ucs-2', 'ut16'):</div><div> return 'public.utf16-plain-text'</div><div> return None</div><div> def canConvert(self, mime, flav):</div><div> return mime.startswith('text/plain') and \</div><div> flav in ('public.utf8-plain-text', 'public.utf16-plain-text')</div><div> def mimeFor(self, flavor):</div><div> if flavor == 'public.utf8-plain-text':</div><div> return 'text/plain'</div><div> if flavor == 'public.utf16-plain-text':</div><div> return 'text/plain;charset=utf16'</div><div> return None</div><div> def convertToMime(self, mimetype, data, flavor):</div><div> if len(data) > 1:</div><div> raise ValueError('Cannot handle multiple member data')</div><div> first, = data</div><div> if flavor == 'public.utf8-plain-text':</div><div> return first.decode('utf-8')</div><div> if flavor == 'public.utf16-plain-text':</div><div> return first.decode('utf-16')</div><div> raise ValueError('Unhandled MIME type: ' + mimetype)</div><div> def convertFromMime(self, mime, data, flavor):</div><div> string = data.toString()</div><div> if flavor == 'public.utf-8.plain-text':</div><div> return string.encode('utf-8')</div><div> if flavor == 'public.utf16-plain-text':</div><div> return string.encode('utf-16')</div><div> return b''</div></div></blockquote><br></div><div>As I said, flavorFor(...) gets called with 'text/plain'. And convertorName() gets called. But not canConvert(...) and convertFromMime(...).</div><div><br></div><div>Thanks,</div><div>Michael</div></div>