How to properly use a derived class from QTextCodec



  • For interfacing with a POS printer I need to use IBM-852 codepage.
    I have subclassed the QTextCodec and created the following class:

    @
    class QIBM852Codec : public QTextCodec
    {
    public:
    QIBM852Codec();
    ~QIBM852Codec();
    QByteArray name() const {return "IBM852";}
    QStringList aliases();
    int mibEnum() const {return 2010;}
    QString convertToUnicode(const char *in, int length, ConverterState *state) const ;
    QByteArray convertFromUnicode(const QChar *in, int length, ConverterState *state) const ;
    private:
    QTextCodec *IBM850Codec;

    };
    @

    Because there are a very few differences between the IBM-850 and IBM-852 I have implemneted it in the following manner:

    @
    #include "qibm852codec.h"

    QIBM852Codec::QIBM852Codec():
    QTextCodec()
    {
    IBM850Codec = QTextCodec::codecForName("IBM850");
    }

    QStringList QIBM852Codec::aliases()
    {
    QStringList ret;
    ret << "ibm-852_P100-1995"
    << "ibm-852"
    << "IBM852"
    << "cp852"
    << "852"
    << "csPCp852"
    << "windows-852";
    return ret;
    }

    QString QIBM852Codec::convertToUnicode(const char *in, int length, ConverterState *state) const
    {
    if (IBM850Codec == NULL) {
    return "";
    } else {
    return IBM850Codec->toUnicode(in, length, state);
    }
    }

    QByteArray QIBM852Codec::convertFromUnicode(const QChar *in, int length, ConverterState *) const
    {
    if (IBM850Codec == NULL) {
    return "";
    } else {
    QByteArray ret;
    for (int i = 0; i<length; i++) {
    switch (in[i].unicode()) {
    case 369: // ű
    ret.append(251);
    break;
    case 337: // ő
    ret.append(139);
    break;
    case 368: // Ű
    ret.append(235);
    break;
    case 336: // Ő
    ret.append(138);
    break;
    default:
    ret.append(IBM850Codec->fromUnicode(&in[i], 1));
    break;
    }
    }
    return ret;
    }

    }

    QIBM852Codec::~QIBM852Codec()
    {

    }
    @

    I have created a static instance in my application, and use in the following way in my application:
    @
    QTextCodec codec = QTextCodec::codecForName("IBM852");
    ret.append(codec->fromUnicode(it.fragment().text()));
    @
    and it works fine.

    But when I quit from my application I got double free corruption.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.