【Qt】 解决QString的sprintf()中文乱码问题

原文链接: http://blog.chinaunix.net/uid-23381466-id-4505611.html

一、解决方法

1、

QString str;
QString tip = QString::fromLocal8Bit("中国人口总数:");
QString tip2 = QString::fromUtf8(tip.toUtf8().data());
str.sprintf("%s %d",tip2.toUtf8().data(), 14);
textEdit->setPlainText (str);

2、

QString str;
QString tip = QString::fromLocal8Bit("中国人口总数:");
str.sprintf("%s %d", tip.toUtf8().data(), 14);
textEdit->setPlainText (str);

二、原理

1、fromLocal8Bit()函数(本段来源

Qt fromLocal8Bit()函数可以设置编码。
Qt默认的编码是unicode,不能显示中文的
windows默认使用(GBK/GB2312/GB18030)
使用fromLocal8Bit()函数,实现了从本地字符集GB到Unicode的转换,用于处理汉语显示乱码等问题

static inline QString fromLocal8Bit(const QByteArray &str);该函数返回的是String类型的数。

猜你喜欢

转载自blog.csdn.net/weixin_44591035/article/details/102569482