Qt设置编码格式 QTextCodec::setCodecForLocale(QTextCodec::codecForName(“UTF-8“));

    技术2022-07-10  144

    设置Qt编码格式

    // 设置编码格式为 UTF-8 QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); // Qt默认会使用本机编码,所以对于中文系统,下面这句设置是多余的 QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));

    以下内容来自官方手册

    [static] void QTextCodec::setCodecForLocale(QTextCodec *c)

    将编解码器设置为c; 这将由codecForLocale()返回。 如果c为nullptr,则将编解码器重置为默认值 Set the codec to c; this will be returned by codecForLocale(). If 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.

    警告:此函数不可重入 Warning: This function is not reentrant.

    另请参见codecForLocale() See also codecForLocale().

    [static] QTextCodec *QTextCodec::codecForLocale()

    // 返回最适合此语言环境的编解码器的指针 Returns a pointer to the codec most suitable for this locale.

    // 编解码器将从使用该后端的ICU中检索,否则可以从特定于操作系统的API中获得。 在后一种情况下,编解码器的名称可以是“系统” 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”.

    // 注意:此函数是线程安全的 Note: This function is thread-safe.

    // 另请参见setCodecForLocale() See also setCodecForLocale().

    Processed: 0.010, SQL: 9