QString::toWCharArray方法说明
环境:mingw32、QT5.2.1
QString::toWCharArray方法用于将QString类型转换成wchar_t类型的数组,但是转换后数组末尾不会加上结束符 '\0',如果不加处理,在做其他操作时,例如将QString转换成char*后,转换的结果末尾会多出一些奇怪的字符。所以需要我们手动加上结束符 '\0'。
QString str = "ceshi";
wchar_t *pWideChar = new wchar_t[str.size()+1];
int actualLen = str.toWCharArray(pWideChar);
// 加结束符'\0'
pWideChar[actualLen] = '\0';
// 转换char*操作
...
...
...
————————————————
版权声明:本文为CSDN博主「chenluhe123」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/chenluhe123/article/details/106816527
QString::toWCharArray可以拷贝到宽字符串里
wchar_t * sourcepath=new wchar_t[MAX_PATH];
int s1=str.toWCharArray(sourcepath);
sourcepath[s1]=0;
https://www.cnblogs.com/findumars/p/6005471.html
==================================
QString::toWCharArray 与 宽字符路径、空格 (测试OK)(***)
//_wsystem(labelWch); 为 win的app,无法在linux下运行?
QString labelText = "C:/test/新建文件夹\" \"-\" \"副本/test.bat"; //OK::注意空格的表示法
//QString labelText = "C:/test/新建文件夹 - 副本/test.bat"; //NG::空格不能直接使用。
wchar_t labelWch[255] = {0};
//将 labelText 转换为 wchar_t,结果放在 labelWch[255]。
int length = labelText.toWCharArray(labelWch);
_wsystem(labelWch);
//_wsystem(labelWch); 为 win的app,无法在linux下运行?
QString labelText = "C:/test/新建文件夹\" \"-\" \"副本/test.bat"; //OK::注意空格的表示法
/// \"(空格)\"
//QString labelText = "C:/test/新建文件夹 - 副本/test.bat"; //NG::空格不能直接使用。
wchar_t labelWch[255] = {0};
//将 labelText 转换为 wchar_t,结果放在 labelWch[255]。
int length = labelText.toWCharArray(labelWch); /// ??????
_wsystem(labelWch);
官方手册
Fills the array with the data contained in this QString object. The array is encoded in UTF-16 on platforms where wchar_t is 2 bytes wide (e.g. windows) and in UCS-4 on platforms where wchar_t is 4 bytes wide (most Unix systems).
array has to be allocated by the caller and contain enough space to hold the complete string (allocating the array with the same length as the string is always sufficient).
This function returns the actual length of the string in array.
Note: This function does not append a null character to the array.
This function was introduced in Qt 4.2.
See also utf16(), toUcs4(), toLatin1(), toUtf8(), toLocal8Bit(), toStdWString(), and QStringView::toWCharArray().