[PyQt] How to create a codec class?

David Boddie dboddie at trolltech.com
Thu Dec 6 19:19:34 GMT 2007


On Thu Dec 6 16:48:00 GMT 2007, DDGG wrote:

> 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

You need to call the __init__() method of the base class; for example:

    def __init__(self):
        QtCore.QTextCodec.__init__(self)

If you miss out this step for QObject subclasses, you'll get an error like
the one you reported:

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

This should probably go in an FAQ somewhere.

David
-- 
David Boddie
Lead Technical Writer, Trolltech ASA


More information about the PyQt mailing list