我有一个 std::string content 我知道包含UTF-8数据。我想将其转换为 QString. 。我该怎么做,避免QT中的from-ascii转换?

有帮助吗?

解决方案

有个 QString 调用函数 fromUtf8 那需要 const char*:

QString str = QString::fromUtf8(content.c_str());

其他提示

QString::fromStdString(content) 更好,因为它更健壮。另请注意,如果 std::string 在UTF-8中编码,然后给出与 QString::fromUtf8(content.data(), int(content.size())).

通常,进行转换的最好方法是使用该方法 Fromutf8, ,但问题是当您具有弦乐位置依赖性时。

在这些情况下,最好使用 来自local8bit. 。例子:

std::string str = "ëxample";
QString qs = QString::fromLocal8Bit(str.c_str());

自从 QT5 从Internally Fromutf8使用,因此您可以同时使用:

inline QString QString::fromStdString(const std::string& s) 
{
return fromUtf8(s.data(), int(s.size()));
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top