|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.internal.QSignalEmitterInternal
com.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.core.QTextCodec
public abstract class QTextCodec
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:
QByteArray encodedString = new QByteArray("..."); QTextCodec codec = QTextCodec.codecForName("KOI8-R"); String 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:
String string = new 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(); String string = new 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.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.
name() | Returns the official name for the encoding. If the encoding is listed in the IANA character-sets encoding file, the name should be the preferred MIME name for the encoding. |
aliases() | Returns a list of alternative names for the encoding. QTextCodec provides a default implementation that returns an empty list. For example, "ISO-8859-1" has "latin1", "CP819", "IBM819", and "iso-ir-100" as aliases. |
mibEnum() | Return the MIB enum for the encoding if it is listed in the IANA character-sets encoding file. |
convertToUnicode() | Converts an 8-bit character string to Unicode. |
convertFromUnicode() | Converts a Unicode string to an 8-bit character string. |
QTextStream
, QTextDecoder
, QTextEncoder
, and Codecs Example.
Nested Class Summary | |
---|---|
static class |
QTextCodec.ConversionFlag
|
static class |
QTextCodec.ConversionFlags
|
Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal |
Field Summary |
---|
Fields inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
currentSender |
Constructor Summary | |
---|---|
protected |
QTextCodec()
Constructs a QTextCodec, and gives it the highest precedence. |
Method Summary | |
---|---|
java.util.List |
aliases()
Subclasses can return a number of aliases for the codec in question. |
static java.util.List |
availableCodecs()
Returns the list of all available codecs, by name. |
static java.util.List |
availableMibs()
Returns the list of MIBs for all available codecs. |
boolean |
canEncode(char arg__1)
Returns true if the Unicode character ch can be fully encoded with this codec; otherwise returns false. |
boolean |
canEncode(java.lang.String arg__1)
This is an overloaded member function, provided for convenience. |
static QTextCodec |
codecForCStrings()
Returns the codec used by QString to convert to and from const char * and QByteArrays. |
static QTextCodec |
codecForHtml(QByteArray ba)
This is an overloaded member function, provided for convenience. |
static QTextCodec |
codecForHtml(QByteArray ba,
QTextCodec defaultCodec)
Tries to detect the encoding of the provided snippet of html in the given byte array, ba, and returns a QTextCodec instance that is capable of decoding the html to unicode. |
static QTextCodec |
codecForLocale()
Returns a pointer to the codec most suitable for this locale. |
static QTextCodec |
codecForMib(int mib)
Returns the QTextCodec which matches the MIBenum mib. |
static QTextCodec |
codecForName(QByteArray name)
Searches all installed QTextCodec objects and returns the one which best matches name; the match is case-insensitive. |
static QTextCodec |
codecForName(java.lang.String name)
Searches all installed QTextCodec objects and returns the one which best matches name; the match is case-insensitive. |
protected abstract QByteArray |
convertFromUnicode(char[] in,
int length,
QTextCodec_ConverterState state)
QTextCodec subclasses must reimplement this function. |
protected abstract java.lang.String |
convertToUnicode(byte[] in,
int length,
QTextCodec_ConverterState state)
QTextCodec subclasses must reimplement this function. |
QByteArray |
fromUnicode(java.lang.String uc)
Converts str from Unicode to the encoding of this codec, and returns the result in a QByteArray . |
QTextDecoder |
makeDecoder()
Creates a QTextDecoder which stores enough state to decode chunks of char * data to create chunks of Unicode data. |
QTextEncoder |
makeEncoder()
Creates a QTextEncoder which stores enough state to encode chunks of Unicode data as char * data. |
abstract int |
mibEnum()
Subclasses of QTextCodec must reimplement this function. |
abstract QByteArray |
name()
QTextCodec subclasses must reimplement this function. |
static void |
setCodecForCStrings(QTextCodec c)
Sets the codec used by QString to convert to and from const char * and QByteArrays. |
static void |
setCodecForLocale(QTextCodec c)
Set the codec to c; this will be returned by codecForLocale() . |
java.lang.String |
toUnicode(QByteArray arg__1)
Converts a from the encoding of this codec to Unicode, and returns the result in a QString. |
Methods inherited from class com.trolltech.qt.QtJambiObject |
---|
dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr |
Methods inherited from class com.trolltech.qt.QSignalEmitter |
---|
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread |
Methods inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
__qt_signalInitialization |
Methods inherited from class java.lang.Object |
---|
clone, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface com.trolltech.qt.QtJambiInterface |
---|
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership |
Constructor Detail |
---|
protected QTextCodec()
Method Detail |
---|
public final boolean canEncode(char arg__1)
public final boolean canEncode(java.lang.String arg__1)
s contains the string being tested for encode-ability.
public final QByteArray fromUnicode(java.lang.String uc)
QByteArray
.
public final QTextDecoder makeDecoder()
QTextDecoder
which stores enough state to decode chunks of char * data to create chunks of Unicode data. The caller is responsible for deleting the returned object.
public final QTextEncoder makeEncoder()
QTextEncoder
which stores enough state to encode chunks of Unicode data as char * data. The caller is responsible for deleting the returned object.
public final java.lang.String toUnicode(QByteArray arg__1)
public java.util.List aliases()
Standard aliases for codecs can be found in the IANA character-sets encoding file.
protected abstract QByteArray convertFromUnicode(char[] in, int length, QTextCodec_ConverterState state)
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.
protected abstract java.lang.String convertToUnicode(byte[] in, int length, QTextCodec_ConverterState state)
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.
public abstract int mibEnum()
public abstract QByteArray name()
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.
public static java.util.List availableCodecs()
QTextCodec::codecForName()
to obtain the QTextCodec for the name. The list may contain many mentions of the same codec if the codec has aliases.
availableMibs()
, name()
, and aliases()
.
public static java.util.List availableMibs()
QTextCodec::codecForMib()
to obtain the QTextCodec for the MIB. availableCodecs()
, and mibEnum()
.
public static QTextCodec codecForCStrings()
setCodecForCStrings()
.
public static QTextCodec codecForHtml(QByteArray ba)
If the codec cannot be detected, this overload returns a Latin-1 QTextCodec.
public static QTextCodec codecForHtml(QByteArray ba, QTextCodec defaultCodec)
public static QTextCodec codecForLocale()
On Windows, the codec will be based on a system locale. On Unix systems, starting with Qt 4.2, the codec will be using the iconv library. Note that in both cases the codec's name will be "System".
setCodecForLocale()
.
public static QTextCodec codecForMib(int mib)
MIBenum
mib.
public static QTextCodec codecForName(QByteArray name)
public static void setCodecForCStrings(QTextCodec c)
Warning: Some codecs do not preserve the characters in the ASCII range (0x00 to 0x7F). For example, the Japanese Shift-JIS encoding maps the backslash character (0x5A) to the Yen character. To avoid undesirable side-effects, we recommend avoiding such codecs with setCodecsForCString().
codecForCStrings()
, and setCodecForTr().
public static void setCodecForLocale(QTextCodec c)
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.
Setting this codec is not supported on DOS based Windows.
codecForLocale()
.
public static QTextCodec codecForName(java.lang.String name)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |