Logo Search packages:      
Sourcecode: qt4-x11 version File versions  Download package

QTextCodec Class Reference

Inheritance diagram for QTextCodec:

QEucJpCodec QEucKrCodec QGb18030Codec QIsciiCodec QJisCodec QSjisCodec QTsciiCodec QGb2312Codec QGbkCodec

List of all members.

Detailed Description

The QTextCodec class provides conversions between text encodings.

Qt uses Unicode to store, draw and manipulate strings. In many situations you may wish to deal with data that uses a different encoding. For example, most Japanese documents are still stored in Shift-JIS or ISO 2022-JP, while Russian users often have their documents in KOI8-R or Windows-1251.

Qt provides a set of QTextCodec classes to help with converting non-Unicode formats to and from Unicode. You can also create your own codec classes.

The supported encodings are:

Apple Roman {Big5 Text Codec}{Big5} {Big5-HKSCS Text Codec}{Big5-HKSCS} {EUC-JP Text Codec}{EUC-JP} {EUC-KR Text Codec}{EUC-KR} {GBK Text Codec}{GB18030-0} IBM 850 IBM 866 IBM 874 {ISO 2022-JP (JIS) Text Codec}{ISO 2022-JP} ISO 8859-1 to 10 ISO 8859-13 to 16 Iscii-Bng, Dev, Gjr, Knd, Mlm, Ori, Pnj, Tlg, and Tml JIS X 0201 JIS X 0208 KOI8-R KOI8-U MuleLao-1 ROMAN8 {Shift-JIS Text Codec}{Shift-JIS} TIS-620 {TSCII Text Codec}{TSCII} UTF-8 UTF-16 UTF-16BE UTF-16LE Windows-1250 to 1258 WINSAMI2

QTextCodecs can be used as follows to convert some locally encoded string to Unicode. Suppose you have some string encoded in Russian KOI8-R encoding, and want to convert it to Unicode. The simple way to do it is like this:

        QByteArray encodedString = "...";
        QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
        QString string = codec->toUnicode(encodedString);

After this, string holds the text converted to Unicode. Converting a string from Unicode to the local encoding is just as easy:

        QString string = "...";
        QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
        QByteArray encodedString = codec->fromUnicode(string);

To read or write files in various encodings, use QTextStream and its {QTextStream::setCodec()}{setCodec()} function. See the {tools/codecs}{Codecs} example for an application of QTextCodec to file I/O.

Some care must be taken when trying to convert the data in chunks, for example, when receiving it over a network. In such cases it is possible that a multi-byte character will be split over two chunks. At best this might result in the loss of a character and at worst cause the entire conversion to fail.

The approach to use in these situations is to create a QTextDecoder object for the codec and use this QTextDecoder for the whole decoding process, as shown below:

        QTextCodec *codec = QTextCodec::codecForName("Shift-JIS");
        QTextDecoder *decoder = codec->makeDecoder();

        QString string;
        while (new_data_available()) {
            QByteArray chunk = get_new_data();
            string += decoder->toUnicode(chunk);

The QTextDecoder object maintains state between chunks and therefore works correctly even if a multi-byte character is split between chunks.

Definition at line 62 of file qtextcodec.h.

Public Types

enum  ConversionFlag { DefaultConversion, ConvertInvalidToNull = 0x80000000, IgnoreHeader = 0x1 }

Public Member Functions

virtual QList< QByteArrayaliases () const
bool canEncode (const QString &) const
bool canEncode (QChar) const
QByteArray fromUnicode (const QChar *in, int length, ConverterState *state=0) const
QByteArray fromUnicode (const QString &uc) const
QTextDecodermakeDecoder () const
QTextEncodermakeEncoder () const
virtual int mibEnum () const =0
virtual QByteArray name () const =0
QString toUnicode (const char *in, int length, ConverterState *state=0) const
QString toUnicode (const char *chars) const
QString toUnicode (const QByteArray &) const

Static Public Member Functions

static QList< QByteArrayavailableCodecs ()
static QList< int > availableMibs ()
static QTextCodeccodecForCStrings ()
static QTextCodeccodecForHtml (const QByteArray &ba)
static QTextCodeccodecForLocale ()
static QTextCodeccodecForMib (int mib)
static QTextCodeccodecForName (const char *name)
static QTextCodeccodecForName (const QByteArray &name)
static QTextCodeccodecForTr ()
static void setCodecForCStrings (QTextCodec *c)
static void setCodecForLocale (QTextCodec *c)
static void setCodecForTr (QTextCodec *c)

Protected Member Functions

virtual QByteArray convertFromUnicode (const QChar *in, int length, ConverterState *state) const =0
virtual QString convertToUnicode (const char *in, int length, ConverterState *state) const =0
 QTextCodec ()
virtual ~QTextCodec ()

Static Private Attributes

static QTextCodeccftr = 0


class QTextCodecCleanup


struct  ConverterState

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index