C++ QString到std::string
我正在尝试该示例代码,但它在visualc++2010中崩溃了C++ QString到std::string,c++,qt,visual-c++,qstring,C++,Qt,Visual C++,Qstring,我正在尝试该示例代码,但它在visualc++2010中崩溃了 QString fileName = "helloworld"; std::string str = fileName.toStdString(); 将QString转换为时,您应该记住的一件事 string是指QString是UTF-16编码的,而 字符串。。。可能有任何编码 std::string str(fileName.toStdString())如果希望str包含qstring的副本visual studio知道什么是
QString fileName = "helloworld";
std::string str = fileName.toStdString();
将QString转换为时,您应该记住的一件事
string是指QString是UTF-16编码的,而
字符串。。。可能有任何编码
std::string str(fileName.toStdString())代码>如果希望str包含qstring的副本visual studio知道什么是qstring吗?我相信您需要将应用程序与某个qt库链接。@izomorphius,我想如果它编译了,那么VS“知道”什么是QString。您的qt库是否可能是根据与编译器安装使用的不同的std::string
编译的?例如,如果Qt库是针对VC2008编译的,那么可能会导致您看到的崩溃。特别是对于VC2010,其中标准库的(IIRC)重要块已更改为更好地匹配C++11..TostString()内部调用toAscii(),因此您不需要知道qt使用了什么itself@Martin贝克特-谢谢,你说得对。另外:我忘了提到“QtTextCodec”类:
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();