QString::toWCharArray 与 宽字符路径、空格 (测试OK)(***)

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);

官方手册

int QString::toWCharArray(wchar_t *array) const

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().

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值