• (五)Qt5之中文显示


        Qt中的中文显示,经常会出现乱码,但在UI设计界面上添加的中文是不会出现乱码的,如果你刚使用qt,那么你肯定会碰到这个问题。

    网上搜索一下,找到的都是这种:

    #include < QTextCodec >
    
    
    
    int main(int argc, char **argv)
    
    {
    
    ....................
    
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF8"));
    
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF8"));
    
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF8"));
    
    ..........................
    
    }
    

    Qt5中,一些函数已经被取消了,而且网上很多都是不推荐这种写法。所以当时找到的是自行转换:

        QTextCodec * BianMa = QTextCodec::codecForName ( "GBK" );
    
        QMessageBox::information(this, "提示", BianMa->toUnicode("中文显示!"));

    image

    其实也可以通过QString定义的静态函数,先转换成Unicode类型:

    QString::fromLocal8Bit("提示")

    不过在Qt5中,提供了一个专门的处理宏,来支持中文常量,那就是QStringLiteral,但它只能处理常量。

    QMessageBox::information(this, QString::fromLocal8Bit("提示"), QStringLiteral("中文显示"));

    image

        const char* info = "中文显示";
    
        //不支持
    
        QString strInfo = QStringLiteral(info);
    
        //支持
    
        QString strInfo = QString::fromLocal8Bit(info);

    对于中文常量,使用QStringLiteral即可解决,对于字符串变量,使用QString自带函数也可以轻松解决。

  • 相关阅读:
    Codeforces 1316B String Modification
    Codeforces 1305C Kuroni and Impossible Calculation
    Codeforces 1305B Kuroni and Simple Strings
    Codeforces 1321D Navigation System
    Codeforces 1321C Remove Adjacent
    Codeforces 1321B Journey Planning
    Operating systems Chapter 6
    Operating systems Chapter 5
    Abandoned country HDU
    Computer HDU
  • 原文地址:https://www.cnblogs.com/lingluotianya/p/3643540.html
Copyright © 2020-2023  润新知