0

I'm have the following in main.cpp file,

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


    QTextCodec::setCodecForCString(QTextCodec::codecForName("UTF-8"));

    MainWindow w;
    w.show();

    return a.exec();
}

And the following is the mainwindow.cpp

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

    result = new QLabel("Check Result Here:",this);

    result->setGeometry(QRect(QPoint(180,30),QSize(161,161)));

    QString m("宁愿看见两个恶魔在拔河,也不愿看见一只天使在跳舞。");

    result->setText(m);

}

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

The Chinese character on the QLabel can not be displayed normally.

If I modify them into follows, the character can be displayed normally.

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

    MainWindow w;
    w.show();

    return a.exec();
}

The following is the mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);    
    QTextCodec::setCodecForCString(QTextCodec::codecForName("UTF-8"));

    result = new QLabel("Check Result Here:",this);

    result->setGeometry(QRect(QPoint(180,30),QSize(161,161)));

    QString m("宁愿看见两个恶魔在拔河,也不愿看见一只天使在跳舞。");

    result->setText(m);

}

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

I think its the problem of the scope of function 'setCodeForCString()`, Can someone explain that?

henryyao
  • 1,758
  • 6
  • 23
  • 37
  • Have you seen this : http://stackoverflow.com/questions/5288959/convert-qstring-into-qbytearray-with-either-utf-8-or-latin1-encoding – user2672165 Aug 15 '13 at 20:30
  • Does the `QMainWindow` constructor set the codec? – IronMensan Aug 15 '13 at 20:49
  • I get the problem now. Its not about the scope of setCodeForCString(), the scope of this function is application level global(95% positive?). The reason of showing the abnormal character is that I did not set the character font in my application. I guess there is no default font for characters other English words... Once I set the font, the character can be displayed correctly. Thanks god, wasted two days on that... – henryyao Aug 15 '13 at 21:55

0 Answers0