3

我正在尝试该示例代码,但它在 visualc++ 2010 中崩溃了

QString fileName = "helloworld";
std::string str = fileName.toStdString();
4

2 回答 2

10

如何将 QString 转换为 std::string?

将 QString 转换为 std::string 时应该记住的一件事是,QString 是 UTF-16 编码的,而 std::string... 可能有任何编码。

QString qs;

// Either this if you use UTF-8 anywhere
std::string utf8_text = qs.toUtf8().constData();

// or this if you on Windows :-)
std::string current_locale_text = qs.toLocal8Bit().constData();
于 2012-06-25T15:01:51.370 回答
1

std::string str(fileName.toStdString());如果您希望 str 包含 qstring 的副本

于 2012-06-25T15:01:53.990 回答