QTextCodec ClassThe QTextCodec class provides conversions between text encodings. More... #include <QTextCodec> Note: All functions in this class are reentrant, except for ~QTextCodec(), which is nonreentrant. Public Types
Public Functions
Static Public Members
Protected Functions
Detailed DescriptionThe 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:
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 setCodec() function. See the 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); } delete decoder; The QTextDecoder object maintains state between chunks and therefore works correctly even if a multi-byte character is split between chunks. Creating Your Own Codec ClassSupport for new text encodings can be added to Qt by creating QTextCodec subclasses. The pure virtual functions describe the encoder to the system and the coder is used as required in the different text file formats supported by QTextStream, and under X11, for the locale-specific character input and output. To add support for another encoding to Qt, make a subclass of QTextCodec and implement the functions listed in the table below.
See also QTextStream, QTextDecoder, QTextEncoder, and Codecs Example. Member Type Documentation
flags QTextCodec::ConversionFlags |
Constant | Value | Description |
---|---|---|
QTextCodec::DefaultConversion | 0 | No flag is set. |
QTextCodec::ConvertInvalidToNull | 0x80000000 | If this flag is set, each invalid input character is output as a null character. |
QTextCodec::IgnoreHeader | 0x1 | Ignore any Unicode byte-order mark and don't generate any. |
The ConversionFlags type is a typedef for QFlags<ConversionFlag>. It stores an OR combination of ConversionFlag values.
Constructs a QTextCodec, and gives it the highest precedence. The QTextCodec should always be constructed on the heap (i.e. with new). Qt takes ownership and will delete it when the application terminates.
Destroys the QTextCodec. Note that you should not delete codecs yourself: once created they become Qt's responsibility.
Warning: This function is not reentrant.
Subclasses can return a number of aliases for the codec in question.
Standard aliases for codecs can be found in the IANA character-sets encoding file.
Returns the list of all available codecs, by name. Call QTextCodec::codecForName() to obtain the QTextCodec for the name.
The list may contain many mentions of the same codec if the codec has aliases.
See also availableMibs(), name(), and aliases().
Returns the list of MIBs for all available codecs. Call QTextCodec::codecForMib() to obtain the QTextCodec for the MIB.
See also availableCodecs() and mibEnum().
Returns true if the Unicode character ch can be fully encoded with this codec; otherwise returns false.
This is an overloaded function.
s contains the string being tested for encode-ability.
Tries to detect the encoding of the provided snippet of HTML in the given byte array, ba, by checking the BOM (Byte Order Mark) and the content-type meta header and returns a QTextCodec instance that is capable of decoding the html to unicode. If the codec cannot be detected from the content provided, defaultCodec is returned.
This function was introduced in Qt 4.4.
See also codecForUtfText().
This is an overloaded function.
Tries to detect the encoding of the provided snippet of HTML in the given byte array, ba, by checking the BOM (Byte Order Mark) and the content-type meta header and returns a QTextCodec instance that is capable of decoding the html to unicode. If the codec cannot be detected, this overload returns a Latin-1 QTextCodec.
Returns a pointer to the codec most suitable for this locale.
On Windows, the codec will be based on a system locale. On Unix systems, the codec will might fall back to using the iconv library if no builtin codec for the locale can be found.
Note that in these cases the codec's name will be "System".
See also setCodecForLocale().
Returns the QTextCodec which matches the MIBenum mib.
Searches all installed QTextCodec objects and returns the one which best matches name; the match is case-insensitive. Returns 0 if no codec matching the name name could be found.
Searches all installed QTextCodec objects and returns the one which best matches name; the match is case-insensitive. Returns 0 if no codec matching the name name could be found.
Tries to detect the encoding of the provided snippet ba by using the BOM (Byte Order Mark) and returns a QTextCodec instance that is capable of decoding the text to unicode. If the codec cannot be detected from the content provided, defaultCodec is returned.
This function was introduced in Qt 4.6.
See also codecForHtml().
This is an overloaded function.
Tries to detect the encoding of the provided snippet ba by using the BOM (Byte Order Mark) and returns a QTextCodec instance that is capable of decoding the text to unicode. If the codec cannot be detected, this overload returns a Latin-1 QTextCodec.
See also codecForHtml().
QTextCodec subclasses must reimplement this function.
Converts the first number of characters from the input array from Unicode to the encoding of the subclass, and returns the result in a QByteArray.
state can be 0 in which case the conversion is stateless and default conversion rules should be used. If state is not 0, the codec should save the state after the conversion in state, and adjust the remainingChars and invalidChars members of the struct.
QTextCodec subclasses must reimplement this function.
Converts the first len characters of chars from the encoding of the subclass to Unicode, and returns the result in a QString.
state can be 0, in which case the conversion is stateless and default conversion rules should be used. If state is not 0, the codec should save the state after the conversion in state, and adjust the remainingChars and invalidChars members of the struct.
Converts str from Unicode to the encoding of this codec, and returns the result in a QByteArray.
Converts the first number of characters from the input array from Unicode to the encoding of this codec, and returns the result in a QByteArray.
The state of the convertor used is updated.
Creates a QTextDecoder with a specified flags to decode chunks of char * data to create chunks of Unicode data.
The caller is responsible for deleting the returned object.
This function was introduced in Qt 4.7.
Creates a QTextEncoder with a specified flags to encode chunks of Unicode data as char * data.
The caller is responsible for deleting the returned object.
This function was introduced in Qt 4.7.
Subclasses of QTextCodec must reimplement this function. It returns the MIBenum (see IANA character-sets encoding file for more information). It is important that each QTextCodec subclass returns the correct unique value for this function.
QTextCodec subclasses must reimplement this function. It returns the name of the encoding supported by the subclass.
If the codec is registered as a character set in the IANA character-sets encoding file this method should return the preferred mime name for the codec if defined, otherwise its name.
Set the codec to c; this will be returned by codecForLocale(). If c is a null pointer, the codec is reset to the default.
This might be needed for some applications that want to use their own mechanism for setting the locale.
See also codecForLocale().
Converts a from the encoding of this codec to Unicode, and returns the result in a QString.
Converts the first size characters from the input from the encoding of this codec to Unicode, and returns the result in a QString.
The state of the convertor used is updated.
This is an overloaded function.
chars contains the source characters.