32位Qt Creator使用Mingw32位能输出中文?为何64位不行(使用Mingw64位)?


#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    QString str("中文");

    qDebug()<<str;
    qDebug()<<QString::fromUtf8("中文");
    //qDebug()<<QString::fromLocal8Bit("中文");
}
​
64位 Qt Creater
32位 Qt Creater

 

编码方式的问题,你看一下QTcreater的“工具”--“选项”--“文本编辑器”--“行为”中,文件编码是否设置的UTF-8。

在main.cpp中添加如下代码:(如有帮助,请采纳一下,谢谢。)

#include "mainwindow.h"
#include <QApplication>
#include <QTextCodec>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    //设置编码格式
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    //QTextCodec *codec = QTextCodec::codecForLocale();
    QTextCodec::setCodecForTr(codec);
    QTextCodec::setCodecForLocale(codec);
    QTextCodec::setCodecForCStrings(codec);

   
    MainWindow w;
    w.show();
    return a.exec();
}
QString str("中文"); 

 改成 

QString str = QStringLiteral("中文"); 

试试。