[PyQt] QMacPasteboardMime

Michael Herrmann michael at herrmann.io
Mon Jul 16 17:58:42 BST 2018


Hi all,

I'm trying to work around QTBUG-61562
<https://bugreports.qt.io/browse/QTBUG-61562>: On Mac, this bug prepends
invisible Unicode characters to the clipboard when copy/pasting.

The class QMacPasteboardMime
<https://doc.qt.io/qt-5/qmacpasteboardmime.html> 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.

Does anyone have experience with this? Here is my code, adapted from a
solution posted in the discussion of QTBUG-61652:

from PyQt5.QtMacExtras import QMacPasteboardMime

class MyMime(QMacPasteboardMime):
    def __init__(self):
        super().__init__(QMacPasteboardMime.MIME_CLIP)
    def convertorName(self):
        return 'UnicodeTextUtf8Default'
    def flavorFor(self, mime):
        if mime == 'text/plain':
            return 'public.utf8-plain-text'
        i = mime.find('charset=')
        if i >= 0:
            charset = mime[i + len('charset='):]
            charset = charset.split(';', 1)[0]
            if charset == 'system':
                return 'public.utf8-plain-text'
            if charset in ('iso-10646-ucs-2', 'ut16'):
                return 'public.utf16-plain-text'
        return None
    def canConvert(self, mime, flav):
        return mime.startswith('text/plain') and \
            flav in ('public.utf8-plain-text', 'public.utf16-plain-text')
    def mimeFor(self, flavor):
        if flavor == 'public.utf8-plain-text':
            return 'text/plain'
        if flavor == 'public.utf16-plain-text':
            return 'text/plain;charset=utf16'
        return None
    def convertToMime(self, mimetype, data, flavor):
        if len(data) > 1:
            raise ValueError('Cannot handle multiple member data')
        first, = data
        if flavor == 'public.utf8-plain-text':
            return first.decode('utf-8')
        if flavor == 'public.utf16-plain-text':
            return first.decode('utf-16')
        raise ValueError('Unhandled MIME type: ' + mimetype)
    def convertFromMime(self, mime, data, flavor):
        string = data.toString()
        if flavor == 'public.utf-8.plain-text':
            return string.encode('utf-8')
        if flavor == 'public.utf16-plain-text':
            return string.encode('utf-16')
        return b''


As I said, flavorFor(...) gets called with 'text/plain'. And
convertorName() gets called. But not canConvert(...) and
convertFromMime(...).

Thanks,
Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180716/5e658305/attachment-0001.html>


More information about the PyQt mailing list