QT5已经移除setCodecForTr等转编码的函数,所以需要用QString::fromStdString和QString::fromLocal8Bit等转换成QString
以下是示例代码
//包含的头文件都被新浪微博自动删除了,自己包含相应的头文件吧
#include
#include
#include
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QProcess pro;
pro.start("demo.exe");
pro.waitForFinished();
string str1 = "111中文";
//QString str = QString(QObject::tr(pro.readAllStandardOutput())); //中文显示乱码
//QString str = QString::fromStdString(pro.readAllStandardOutput()); //中文显示乱码
QByteArray qba = pro.readAllStandardOutput();
char* myChar = new char[qba.length()];
for(int i=0; i i++)
{
myChar[i] = qba[i];
}
string str2 = string(myChar);
delete []myChar;
qDebug()<<QString::fromStdString(str1) //中文显示正常
<<QString::fromStdString(str1.c_str()) //中文显示正常
<<QString::fromStdString(str2) //中文显示乱码
<<QString::fromLocal8Bit(str2.c_str()) //中文显示正常
<<QString::fromLocal8Bit(qba); //中文显示正常
return a.exec();
}