Qstring到LPCWSTR

use*_*719 9 qt

LPCWSTR path;

void WinApiLibrary::StartProcess(QString name)
{
    path = name.utf16();
    CreateProcess(path, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
}
Run Code Online (Sandbox Code Playgroud)

C:\ kursovaya\smc\winapilibrary.cpp:21:错误:从'const ushort*{aka const short unsigned int*}'无效转换为'LPCWSTR {aka const wchar_t*}'[-fpermissive] path = name.utf16 ();

这段代码在Qt 4.8中工作,但现在我有Qt 5.2,这段代码不起作用.这家伙怎么了?

Str*_*ari 17

我有同样的问题(我正在使用Qt 5.3),这是我修复它的方式:

QString strVariable1;
LPCWSTR strVariable2 = (const wchar_t*) strVariable1.utf16();
Run Code Online (Sandbox Code Playgroud)


Pau*_*aul 5

QString::utf16()返回const ushort*,这是不同的const wchar_t*,所以你有编译错误.

你可能正在编译/Zc:wchar_t.如果你改变它/Zc:wchar_t-应该工作,因为wchar_t在这种情况下类型变为typedef到16位整数.

在Visual Studio中:项目属性 - >配置属性 - > C/C++ - >将WChar_t视为内置类型 - >否.

或者只是添加reinterpret_cast<LPCWSTR>.