QTextCodec 類提供文本編碼之間的轉換。 更多...
| 頭: | #include <QTextCodec> |
| qmake: | QT += core |
注意: 此類的所有函數 可重入 .
注意: 這些函數也是 綫程安全 :
| struct | ConverterState |
| enum | ConversionFlag { DefaultConversion, ConvertInvalidToNull, IgnoreHeader } |
| flags | ConversionFlags |
| virtual QList<QByteArray> | aliases () const |
| bool | canEncode (QChar ch ) const |
| bool | canEncode (const QString & s ) const |
| bool | canEncode (QStringView s ) const |
| QByteArray | fromUnicode (const QString & str ) const |
| QByteArray | fromUnicode (QStringView str ) const |
| QByteArray | fromUnicode (const QChar * input , int number , QTextCodec::ConverterState * state = nullptr) const |
| QTextDecoder * | makeDecoder (QTextCodec::ConversionFlags flags = DefaultConversion) const |
| QTextEncoder * | makeEncoder (QTextCodec::ConversionFlags flags = DefaultConversion) const |
| virtual int | mibEnum () const = 0 |
| virtual QByteArray | name () const = 0 |
| QString | toUnicode (const QByteArray & a ) const |
| QString | toUnicode (const char * chars ) const |
| QString | toUnicode (const char * input , int size , QTextCodec::ConverterState * state = nullptr) const |
| QList<QByteArray> | availableCodecs () |
| QList<int> | availableMibs () |
| QTextCodec * | codecForHtml (const QByteArray & ba , QTextCodec * defaultCodec ) |
| QTextCodec * | codecForHtml (const QByteArray & ba ) |
| QTextCodec * | codecForLocale () |
| QTextCodec * | codecForMib (int mib ) |
| QTextCodec * | codecForName (const QByteArray & name ) |
| QTextCodec * | codecForName (const char * name ) |
| QTextCodec * | codecForUtfText (const QByteArray & ba , QTextCodec * defaultCodec ) |
| QTextCodec * | codecForUtfText (const QByteArray & ba ) |
| void | setCodecForLocale (QTextCodec * c ) |
| QTextCodec () | |
| virtual | ~QTextCodec () |
| virtual QByteArray | convertFromUnicode (const QChar * input , int number , QTextCodec::ConverterState * state ) const = 0 |
| virtual QString | convertToUnicode (const char * chars , int len , QTextCodec::ConverterState * state ) const = 0 |
Qt 使用 Unicode 存儲、繪製及操縱字符串。在很多狀況下,可能希望處理使用不同編碼的數據。例如,大多數日語文檔仍然存儲在 Shift-JIS 或 ISO 2022-JP,而俄羅斯用戶經常將其文檔存儲在 KOI8-R 或 Windows-1251。
Qt 提供瞭一組 QTextCodec 類,以幫助轉換非 Unicode 格式到和來自 Unicode。還可以創建自己的編解碼器類。
支持的編碼:
若啓用 ICU 支持編譯 Qt,則 ICU 支持的大多數編解碼器也可用於應用程序。
QTextCodec s 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 及其 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.
Support 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.
| 函數 | 描述 |
|---|---|
| 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 ,和 文本編解碼器範例 .
| 常量 | 值 | 描述 |
|---|---|---|
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.
[protected]
QTextCodec::
QTextCodec
()
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.
[virtual protected]
QTextCodec::
~QTextCodec
()
銷毀 QTextCodec . Note that you should not delete codecs yourself: once created they become Qt's responsibility.
警告: 此函數不 可重入 .
[虛擬]
QList
<
QByteArray
> QTextCodec::
aliases
() const
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 .
[static]
QList
<
QByteArray
> QTextCodec::
availableCodecs
()
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.
注意: 此函數是 綫程安全 .
另請參閱 availableMibs (), name (),和 aliases ().
[static]
QList
<
int
> QTextCodec::
availableMibs
()
Returns the list of MIBs for all available codecs. Call QTextCodec::codecForMib () to obtain the QTextCodec for the MIB.
注意: 此函數是 綫程安全 .
另請參閱 availableCodecs () 和 mibEnum ().
返迴
true
if the Unicode character
ch
can be fully encoded with this codec; otherwise returns
false
.
這是重載函數。
s contains the string being tested for encode-ability.
這是重載函數。
返迴
true
if the Unicode string
s
can be fully encoded with this codec; otherwise returns
false
.
該函數在 Qt 5.10 引入。
[static]
QTextCodec
*QTextCodec::
codecForHtml
(const
QByteArray
&
ba
,
QTextCodec
*
defaultCodec
)
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 被返迴。
該函數在 Qt 4.4 引入。
另請參閱 codecForUtfText ().
[static]
QTextCodec
*QTextCodec::
codecForHtml
(const
QByteArray
&
ba
)
這是重載函數。
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 .
[static]
QTextCodec
*QTextCodec::
codecForLocale
()
Returns a pointer to the codec most suitable for this locale.
The codec will be retrieved from ICU where that backend is in use, otherwise it may be obtained from an OS-specific API. In the latter case, the codec's name may be "System".
注意: 此函數是 綫程安全 .
另請參閱 setCodecForLocale ().
[static]
QTextCodec
*QTextCodec::
codecForMib
(
int
mib
)
返迴 QTextCodec which matches the MIBenum mib .
注意: 此函數是 綫程安全 .
[static]
QTextCodec
*QTextCodec::
codecForName
(const
QByteArray
&
name
)
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.
注意: 此函數是 綫程安全 .
[static]
QTextCodec
*QTextCodec::
codecForName
(const
char
*
name
)
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.
[static]
QTextCodec
*QTextCodec::
codecForUtfText
(const
QByteArray
&
ba
,
QTextCodec
*
defaultCodec
)
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. This function can detect one of the following codecs:
If the codec cannot be detected from the content provided, defaultCodec 被返迴。
該函數在 Qt 4.6 引入。
另請參閱 codecForHtml ().
[static]
QTextCodec
*QTextCodec::
codecForUtfText
(const
QByteArray
&
ba
)
這是重載函數。
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. This function can detect one of the following codecs:
If the codec cannot be detected from the content provided, this overload returns a Latin-1 QTextCodec .
另請參閱 codecForHtml ().
[pure virtual protected]
QByteArray
QTextCodec::
convertFromUnicode
(const
QChar
*
input
,
int
number
,
QTextCodec::ConverterState
*
state
) const
QTextCodec 子類必須重實現此函數。
轉換第一 number of characters from the input array from Unicode to the encoding of the subclass, and returns the result in a QByteArray .
state
可以是
nullptr
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.
[pure virtual protected]
QString
QTextCodec::
convertToUnicode
(const
char
*
chars
,
int
len
,
QTextCodec::ConverterState
*
state
) const
QTextCodec 子類必須重實現此函數。
轉換第一 len 字符的 chars from the encoding of the subclass to Unicode, and returns the result in a QString .
state
可以是
nullptr
, 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.
轉換 str from Unicode to the encoding of this codec, and returns the result in a QByteArray .
這是重載函數。
轉換 str from Unicode to the encoding of this codec, and returns the result in a QByteArray .
該函數在 Qt 5.10 引入。
轉換第一 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.
創建
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.
該函數在 Qt 4.7 引入。
創建
QTextEncoder
with a specified
flags
to encode chunks of Unicode data as
char *
數據。
The caller is responsible for deleting the returned object.
該函數在 Qt 4.7 引入。
[pure virtual]
int
QTextCodec::
mibEnum
() const
子類化的 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.
[pure virtual]
QByteArray
QTextCodec::
name
() const
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.
[static]
void
QTextCodec::
setCodecForLocale
(
QTextCodec
*
c
)
Set the codec to
c
; this will be returned by
codecForLocale
()。若
c
is
nullptr
, 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.
警告: 此函數不 可重入 .
另請參閱 codecForLocale ().
轉換 a from the encoding of this codec to Unicode, and returns the result in a QString .
這是重載函數。
chars contains the source characters.
轉換第一 size 字符來自 input from the encoding of this codec to Unicode, and returns the result in a QString .
The state of the convertor used is updated.