QString到std :: string

时间:2012-06-25 14:56:17

标签: c++ qt visual-c++ qstring

我正在尝试使用示例代码并在visual c ++ 2010中崩溃

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

2 个答案:

答案 0 :(得分:10)

  

How to convert QString to 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();

答案 1 :(得分:1)

std::string str(fileName.toStdString());如果你想让str包含qstring的副本

相关问题