c++ - 删除 unicode def 后无法将 QString 转换为 TCHAR
问题描述
以前当我声明 unicode 时,我能够执行以下操作
QString myPassword = "123";
TCHAR szPassword[32];
myPassword.toWCharArray(szPassword);
但是在我删除了我的 uncode def 之后,我得到了以下错误
error C2664: 'QString::toWCharArray' : cannot convert parameter 1 from
'TCHAR [32]' to 'wchar_t *'
我现在如何将我的 Qstring 转换为 TCHAR 数组?
解决方案
Qt 提供了几种在QString
Windows 标准和 UTF16-LE 之间进行转换的方法:
转换为 C++ std::wstring
:
QString myPassword = "abcd ελληνικά";
std::wstring wstr = myPassword.toStdWString();
MessageBoxW(0, wstr.c_str(),0,0);
对于 using toWCharArray
,输入缓冲区必须有足够的内存并且必须以空值结尾:
wchar_t buf[100] = {0};
myPassword.toWCharArray(buf);
MessageBoxW(0, buf,0,0);
或者使用QString::utf16()
返回的方法const unsigned short*
,它需要const wchar_t*
强制转换:
MessageBoxW(0, (const wchar_t*)myPassword.utf16(), 0, 0);
注意,TCHAR
是一个令人困惑的 Windows 宏,它被定义char
为 ANSI 程序和wchar_t
Unicode 程序。避免使用它。
推荐阅读
- javascript - Bootstrap Datetimepicket:表 td 宽度大小未调整大小
- vb.net - 在 VB.net 中过滤数据
- keyboard-shortcuts - macOS 使用“命令键”作为 zsh 快捷方式
- python - 在需要一段时间才能打开的 tkinter 文件上创建加载消息
- spring - 具有动态目的地的春季云流多接收器
- android - 在 Android 10+ 上从外部存储(包括下载文件夹)获取所有文件
- android - 如果在两种情况下都启用数据绑定,则 binding.name.text && name.text 有什么区别
- plot - Seaborn 双胞胎情节未按预期工作
- c# - 使用 .NET 中的数据绑定项从集合的 id 设置组合框值
- reactjs - 在函数中的 onSubmit 之后添加警报