0

我在 macOS 和 windows 上开发了一个跨平台的 qt 应用程序,我有一个名为“proj.json”的文件,它是由我在 windows 上的应用程序中的 rapidjson 生成的,在 windows 上它的编码格式是 GB2312,现在我在我的mac 上的应用程序在 proj.json 文件中有一个字符串 proj.json,在我的 qt 应用程序中,在 proj.json 文件中有一个字符串。 \Templates22D 魔法师\2D 魔法师\.fuproj'

string pjson = fullfile(folder, "/proj.json");
    if (isexist(pjson))
    {
        rapidjson::Document doc;
        bool isReadJsonSuccess;
        doc = loadJson(pjson,&isReadJsonSuccess);
        if (isReadJsonSuccess){
            rapidjson::Value &ps = doc["Projects"];
            for (int i = 0; i < ps.Size(); i++) {
                rapidjson::Value &item = ps[i];
                string str1 = item["path"].GetString();
                                cout << "str1----" << str1 << endl;


它注销'str1----.\Templates\2D\314\371ֽ\2D\314\371ֽ.fuproj',我没有字符串'.\Templates\2D\314\371ֽ\2D的编码格式\314\371ֽ.fuproj',而我在 mac 运行时编码格式上的 qt 应用程序是 'UTF-8',看来 GB2312 转换为 UTF-8 是因为这个。如何让 GB312 字符串 'Templates\2D.\'2D.\'2D.D.fuproj' 在 macos 上的 qt 应用程序中正确显示,非常感谢!

4

1 回答 1

1

如何通过以下方法转换它

#include <QTextCodec>

inline QString GBK2UTF8(const QString &inStr)
{
    QTextCodec *gbk = QTextCodec::codecForName("GB18030");
    QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");

    QString g2u = gbk->toUnicode(gbk->fromUnicode(inStr));            // gbk  convert utf8
    return g2u;
}

inline QString UTF82GBK(const QString &inStr)
{
    QTextCodec *gbk = QTextCodec::codecForName("GB18030");
    QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");

    QString utf2gbk = gbk->toUnicode(inStr.toLocal8Bit());
    return utf2gbk;
}

// Convert stirng to QString
std::string str = "Hello world";
QString qstr = QString::fromStdString(str);

我没有测试它,霍德可以提供帮助。

于 2019-05-14T03:17:23.550 回答