QString到std :: string

Moa*_*een 3 c++ qstring qt visual-c++

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

QString fileName = "helloworld";
std::string str = fileName.toStdString();
Run Code Online (Sandbox Code Playgroud)

pau*_*sm4 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();
Run Code Online (Sandbox Code Playgroud)

  • .toStdString()在内部调用ascii(),因此您不需要知道qt使用了什么 (2认同)