首页 > 解决方案 > 如何从 QString 中获取 unicode 字符?

问题描述

所以希望这比我想象的要简单。我正在将 JSON 数据读取到我的应用程序中,并以 QString 的形式出现一个条目"\uf103",它实际上由字符'\\''u''f''1''0'和表示'3'。我将如何实际将其转换为由代码表示的 unicode 字符\uf103?我经历了许多似乎提出类似问题的线程,但没有一个有直接的答案。

对于上下文,我只是想将此字符显示为按钮的文本。

我的解析例程如下所示:

    QString infoPath = QDir::currentPath() + "/resources/fonts/fontawesome/unicode_info.json";
    QJsonObject fontAwesome = JsonReader(infoPath).getContentsAsJsonObject();

    QString fa = JsonReader::getJsonValueAsQString(fontAwesome);
    if (!fontAwesome.contains(fontAwesomeIcon)) {
        qDebug() << "Error, font awesome icon not found";
#ifdef DEBUG_MODE
        throw("Error, font awesome icon not found");
#endif
    }
    QString unicode = fontAwesome.value(fontAwesomeIcon).toObject().value("unicode").toString();

字符串unicode是感兴趣的主题。原始 JSON 看起来像这样,只是更大:

{"500px": {"unicode": "\\uf26e", "styles": ["brands"], "label": "500px"}, "accessible-icon": {"unicode": "\\uf368", "styles": ["brands"], "label": "Accessible Icon"}, "accusoft": {"unicode": "\\uf369", "styles": ["brands"], "label": "Accusoft"}, "acquisitions-incorporated": {"unicode": "\\uf6af", "styles": ["brands"], "label": "Acquisitions Incorporated"}}

所以,问题是我首先从双斜杠开始。如果有办法以这种方式获取 unicode 字符,我也愿意在 python 中预处理这个 JSON 的方法。

标签: c++qtunicode

解决方案


我通过 python 处理步骤解决了这个问题,在每个有问题的字符串上运行以下行:

problem_string = "\\u2095"
good_string = bytes(problem_string, "ascii").decode('unicode-escape'), 

推荐阅读