Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QString到std::string_C++_Qt_Visual C++_Qstring - Fatal编程技术网

C++ QString到std::string

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知道什么是

我正在尝试该示例代码,但它在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吗?我相信您需要将应用程序与某个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();