[PyQt] How to create a codec class?

DDGG ddcatgg at gmail.com
Thu Dec 6 16:48:00 GMT 2007


Hello friends!
I want to create a new codec class.
I browsed the Qt's document, it tells me to make a subclass of
QTextCodec and implement five functions.
So this is my program:

# cncodec.py
from PyQt4 import QtCore

class QGb18030Codec(QtCore.QTextCodec):
    def __init__(self):
        pass

    def name(self):
        return "GB18030"

    def aliases(self):
        return []

    def mibEnum(self):
        return 114

    def convertToUnicode(self, chars, len, state):
        pass  # just test this class first

    def convertFromUnicode(self, uc, len, state):
        pass # just test this class first

gb18030 = QGb18030Codec()
print QtCore.QTextCodec.codecForName("GB18030")
QtCore.QTextCodec.setCodecForCStrings(gb18030)
# end cncodec.py

I ran it and got these output:
None
Traceback (most recent call last):
  File "cncodec.py", line 26, in <module>
    QtCore.QTextCodec.setCodecForCStrings(gb18030)
RuntimeError: underlying C/C++ object has been deleted

What's wrong with my program?
How can i "register" my codec class to PyQt? Since
codecForName("GB18030") returns "None".
Thanks!


More information about the PyQt mailing list