[][src]Struct qt_core::QTextCodec

#[repr(C)]
pub struct QTextCodec { /* fields omitted */ }

The QTextCodec class provides conversions between text encodings.

C++ class: QTextCodec.

C++ documentation:

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:

  • Big5
  • Big5-HKSCS
  • CP949
  • EUC-JP
  • EUC-KR
  • GB18030
  • HP-ROMAN8
  • IBM 850
  • IBM 866
  • IBM 874
  • ISO 2022-JP
  • ISO 8859-1 to 10
  • ISO 8859-13 to 16
  • Iscii-Bng, Dev, Gjr, Knd, Mlm, Ori, Pnj, Tlg, and Tml
  • KOI8-R
  • KOI8-U
  • Macintosh
  • Shift-JIS
  • TIS-620
  • TSCII
  • UTF-8
  • UTF-16
  • UTF-16BE
  • UTF-16LE
  • UTF-32
  • UTF-32BE
  • UTF-32LE
  • Windows-1250 to 1258

If Qt is compiled with ICU support enabled, most codecs supported by ICU will also be available to the application.

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.

Methods

impl QTextCodec[src]

pub unsafe fn aliases(&self) -> CppBox<QListOfQByteArray>[src]

Subclasses can return a number of aliases for the codec in question.

Calls C++ function: virtual QList<QByteArray> QTextCodec::aliases() const.

C++ documentation:

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.

pub unsafe fn available_codecs() -> CppBox<QListOfQByteArray>[src]

Returns the list of all available codecs, by name. Call QTextCodec::codecForName() to obtain the QTextCodec for the name.

Calls C++ function: static QList<QByteArray> QTextCodec::availableCodecs().

C++ documentation:

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.

Note: This function is thread-safe.

See also availableMibs(), name(), and aliases().

pub unsafe fn available_mibs() -> CppBox<QListOfInt>[src]

Returns the list of MIBs for all available codecs. Call QTextCodec::codecForMib() to obtain the QTextCodec for the MIB.

Calls C++ function: static QList<int> QTextCodec::availableMibs().

C++ documentation:

Returns the list of MIBs for all available codecs. Call QTextCodec::codecForMib() to obtain the QTextCodec for the MIB.

Note: This function is thread-safe.

See also availableCodecs() and mibEnum().

pub unsafe fn can_encode_q_char(&self, arg1: impl CastInto<Ref<QChar>>) -> bool[src]

Returns true if the Unicode character ch can be fully encoded with this codec; otherwise returns false.

Calls C++ function: bool QTextCodec::canEncode(QChar arg1) const.

C++ documentation:

Returns true if the Unicode character ch can be fully encoded with this codec; otherwise returns false.

pub unsafe fn can_encode_q_string(
    &self,
    arg1: impl CastInto<Ref<QString>>
) -> bool
[src]

This is an overloaded function.

Calls C++ function: bool QTextCodec::canEncode(const QString& arg1) const.

C++ documentation:

This is an overloaded function.

s contains the string being tested for encode-ability.

pub unsafe fn codec_for_html_1a(
    ba: impl CastInto<Ref<QByteArray>>
) -> MutPtr<QTextCodec>
[src]

This is an overloaded function.

Calls C++ function: static QTextCodec* QTextCodec::codecForHtml(const QByteArray& ba).

C++ documentation:

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.

pub unsafe fn codec_for_html_2a(
    ba: impl CastInto<Ref<QByteArray>>,
    default_codec: impl CastInto<MutPtr<QTextCodec>>
) -> MutPtr<QTextCodec>
[src]

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.

Calls C++ function: static QTextCodec* QTextCodec::codecForHtml(const QByteArray& ba, QTextCodec* defaultCodec).

C++ documentation:

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().

pub unsafe fn codec_for_locale() -> MutPtr<QTextCodec>[src]

Returns a pointer to the codec most suitable for this locale.

Calls C++ function: static QTextCodec* QTextCodec::codecForLocale().

C++ documentation:

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".

Note: This function is thread-safe.

See also setCodecForLocale().

pub unsafe fn codec_for_mib(mib: c_int) -> MutPtr<QTextCodec>[src]

Returns the QTextCodec which matches the MIBenum mib.

Calls C++ function: static QTextCodec* QTextCodec::codecForMib(int mib).

C++ documentation:

Returns the QTextCodec which matches the MIBenum mib.

Note: This function is thread-safe.

pub unsafe fn codec_for_name_q_byte_array(
    name: impl CastInto<Ref<QByteArray>>
) -> MutPtr<QTextCodec>
[src]

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.

Calls C++ function: static QTextCodec* QTextCodec::codecForName(const QByteArray& name).

C++ documentation:

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.

Note: This function is thread-safe.

pub unsafe fn codec_for_name_char(
    name: impl CastInto<Ptr<c_char>>
) -> MutPtr<QTextCodec>
[src]

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.

Calls C++ function: static QTextCodec* QTextCodec::codecForName(const char* name).

C++ documentation:

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.

pub unsafe fn codec_for_utf_text_1a(
    ba: impl CastInto<Ref<QByteArray>>
) -> MutPtr<QTextCodec>
[src]

This is an overloaded function.

Calls C++ function: static QTextCodec* QTextCodec::codecForUtfText(const QByteArray& ba).

C++ documentation:

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().

pub unsafe fn codec_for_utf_text_2a(
    ba: impl CastInto<Ref<QByteArray>>,
    default_codec: impl CastInto<MutPtr<QTextCodec>>
) -> MutPtr<QTextCodec>
[src]

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.

Calls C++ function: static QTextCodec* QTextCodec::codecForUtfText(const QByteArray& ba, QTextCodec* defaultCodec).

C++ documentation:

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().

pub unsafe fn from_unicode_q_string(
    &self,
    uc: impl CastInto<Ref<QString>>
) -> CppBox<QByteArray>
[src]

Converts str from Unicode to the encoding of this codec, and returns the result in a QByteArray.

Calls C++ function: QByteArray QTextCodec::fromUnicode(const QString& uc) const.

C++ documentation:

Converts str from Unicode to the encoding of this codec, and returns the result in a QByteArray.

pub unsafe fn make_decoder_1a(
    &self,
    flags: QFlags<ConversionFlag>
) -> MutPtr<QTextDecoder>
[src]

Creates a QTextDecoder with a specified flags to decode chunks of char * data to create chunks of Unicode data.

Calls C++ function: QTextDecoder* QTextCodec::makeDecoder(QFlags<QTextCodec::ConversionFlag> flags = …) const.

C++ documentation:

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.

pub unsafe fn make_decoder_0a(&self) -> MutPtr<QTextDecoder>[src]

Creates a QTextDecoder with a specified flags to decode chunks of char * data to create chunks of Unicode data.

Calls C++ function: QTextDecoder* QTextCodec::makeDecoder() const.

C++ documentation:

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.

pub unsafe fn make_encoder_1a(
    &self,
    flags: QFlags<ConversionFlag>
) -> MutPtr<QTextEncoder>
[src]

Creates a QTextEncoder with a specified flags to encode chunks of Unicode data as char * data.

Calls C++ function: QTextEncoder* QTextCodec::makeEncoder(QFlags<QTextCodec::ConversionFlag> flags = …) const.

C++ documentation:

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.

pub unsafe fn make_encoder_0a(&self) -> MutPtr<QTextEncoder>[src]

Creates a QTextEncoder with a specified flags to encode chunks of Unicode data as char * data.

Calls C++ function: QTextEncoder* QTextCodec::makeEncoder() const.

C++ documentation:

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.

pub unsafe fn mib_enum(&self) -> c_int[src]

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.

Calls C++ function: pure virtual int QTextCodec::mibEnum() const.

C++ documentation:

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.

pub unsafe fn name(&self) -> CppBox<QByteArray>[src]

QTextCodec subclasses must reimplement this function. It returns the name of the encoding supported by the subclass.

Calls C++ function: pure virtual QByteArray QTextCodec::name() const.

C++ documentation:

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.

pub unsafe fn set_codec_for_locale(c: impl CastInto<MutPtr<QTextCodec>>)[src]

Set the codec to c; this will be returned by codecForLocale(). If c is a null pointer, the codec is reset to the default.

Calls C++ function: static void QTextCodec::setCodecForLocale(QTextCodec* c).

C++ documentation:

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.

Warning: This function is not reentrant.

See also codecForLocale().

pub unsafe fn to_unicode_q_byte_array(
    &self,
    arg1: impl CastInto<Ref<QByteArray>>
) -> CppBox<QString>
[src]

Converts a from the encoding of this codec to Unicode, and returns the result in a QString.

Calls C++ function: QString QTextCodec::toUnicode(const QByteArray& arg1) const.

C++ documentation:

Converts a from the encoding of this codec to Unicode, and returns the result in a QString.

pub unsafe fn to_unicode_char(
    &self,
    chars: impl CastInto<Ptr<c_char>>
) -> CppBox<QString>
[src]

This is an overloaded function.

Calls C++ function: QString QTextCodec::toUnicode(const char* chars) const.

C++ documentation:

This is an overloaded function.

chars contains the source characters.

Auto Trait Implementations

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> StaticUpcast<T> for T[src]

impl<T, U> CastInto<U> for T where
    U: CastFrom<T>, 
[src]