Qt 网络编程汉字乱码怎么解决

Qt 网络编程汉字乱码怎么解决,第1张

// 首先你需要设置编码格式

    // 设置中文字体编码

#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))

    QTextCodec *codec = QTextCodec::codecForName("UTF-8")

    QTextCodec::setCodecForLocale(codec)

    QTextCodec::setCodecForCStrings(codec)

    QTextCodec::setCodecForTr(codec)

#else

    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF8"))

#endif

// 然后再进行字符传输

qt fromLocal8Bit()函数可以设置编码。

QT默认的编码(unicode)是不能显示中文的,可能由于windows的默认编码的问题,windows默认使用(GBK/GB2312/GB18030),所以需要来更改QT程序的编码来解决中文显示的问题。

系统直接自动将char *的参数转换成为系统默认的编码,然后返回一个QString。

#include <QApplication>

#include <QTextCodec>

#include <QLabel>

int main(int argc,char *argv[])

{

QApplication app(argc,argv)

QString str

str = str.fromLocal8Bit("Qt中文显示")

hello.setWindowTitle(str)

hello.show()

return app.exec()

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11042561.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

Processed Time:0.16382002830505

_REQUEST:
Array
(
    [0] => read
    [1] => 11042561
    [rewrite] => tougao/11042561.html
)
_SESSION:
Array
(
    [fid] => 45
)
保存