Arabic in Qt with QString

2019-03-06 17:37发布

I want to add an Arabic title to my Qt application, but it didn't work. Here is my code:

#include "mainwindow.h"
#include <QtGui/QApplication>
#include <QString>
#include <QTextStream>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    mainWindow w;
    QString appTitle("تجربه");
    w.setWindowTitle(appTitle);
    w.show();
    return a.exec();
}

And here is the output:

enter image description here

How can I correct it?

标签: c++ qt arabic
2条回答
等我变得足够好
2楼-- · 2019-03-06 17:55

That looks like a typical "UTF-8 interpreted as ISO-8859-1" encoding issue. In fact it's a "CP1256 interpreted as Latin1" issue.

On Windows, with a non-Unicode codepage, try the following:

QString appTitle = QString::fromLocal8Bit("تجربه");

If you had your source file in UTF-8, try this instead:

QString appTitle = QString::fromUtf8("تجربه");

(See codecForLocale() for what that's supposed to do.)

Qt Creator 2.7/Windows 7 (in a VM)/UTF-8 source file:

enter image description here

查看更多
贪生不怕死
3楼-- · 2019-03-06 18:04

Try this instead. That way the string literal itself will be Unicode for sure:

QString appTitle = QString::fromStdWString(L"تجربه");
查看更多
登录 后发表回答