Arabic in Qt with QString

坚强是说给别人听的谎言 提交于 2019-12-02 16:15:15

问题


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:

How can I correct it?


回答1:


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:




回答2:


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

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



回答3:


set a locale and use QString directly with arabic. In case you want to change the gui to english, you will have to change the locale and use tr in QString.

QString a=tr("تجربه")


来源:https://stackoverflow.com/questions/16436278/arabic-in-qt-with-qstring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!