user3132719
user3132719

Reputation: 163

Qstring to LPCWSTR

LPCWSTR path;

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

C:\kursovaya\smc\winapilibrary.cpp:21: error: invalid conversion from 'const ushort* {aka const short unsigned int*}' to 'LPCWSTR {aka const wchar_t*}' [-fpermissive] path = name.utf16();

This code worked in the Qt 4.8, but now i have Qt 5.2 and this code isn't working. What's wrong with this guy?

Upvotes: 13

Views: 17602

Answers (4)

Matt Eding
Matt Eding

Reputation: 1002

In the Qt global namespace, there is the macro qUtf16Printable. However as the documentation states, this produces a temporary so take care to use it properly. Note this was introduced in Qt5.7.

Upvotes: 0

Krishna Prasad P
Krishna Prasad P

Reputation: 89

I'm using Qt 5.2 and I had the same issue. This is how I fixed it:

QString sPath = "C:\\Program File\\MyProg";
wchar_t wcPath[1024];
int iLen = sPath.toWCharArray(wcPath);

Upvotes: 4

user925861
user925861

Reputation:

I had the same issue (I'm using Qt 5.3), this is how I fixed it:

QString strVariable1;
LPCWSTR strVariable2 = (const wchar_t*) strVariable1.utf16();

Upvotes: 19

Paul
Paul

Reputation: 13238

QString::utf16() returns const ushort*, which is different from const wchar_t*, so you have the compile error.

You are probably compiling with /Zc:wchar_t. If you change it to /Zc:wchar_t- it should work, as wchar_t type becomes typedef to 16-bit integer in this case.

In Visual Studio: Project Properties->Configuration Properties->C/C++->Treat WChar_t As Built in Type->No.

Or just add reinterpret_cast<LPCWSTR>.

Upvotes: 7

Related Questions