Can't convert from QString to LPCTSTR
-
I have:
LPCTSTR tempLPC = (LPCTSTR)someString.toUtf8();
No other Utf i have, in google all say that must be .toUtf16(); but i dont have that. How to convert it?
-
I have:
LPCTSTR tempLPC = (LPCTSTR)someString.toUtf8();
No other Utf i have, in google all say that must be .toUtf16(); but i dont have that. How to convert it?
LPCTSTR
is eitherconst char *
orconst wchar_t *
orconst unsigned short *
depending on compiler macros. So you need to handle the preprocessor first. Usually it goes like this:#ifdef UNICODE LPCWSTR something = someString.utf16(); #else QByteArray latinString = someString.toLatin1(); LPCSTR something = latinString.constData(); #endif
-
LPCTSTR
is eitherconst char *
orconst wchar_t *
orconst unsigned short *
depending on compiler macros. So you need to handle the preprocessor first. Usually it goes like this:#ifdef UNICODE LPCWSTR something = someString.utf16(); #else QByteArray latinString = someString.toLatin1(); LPCSTR something = latinString.constData(); #endif
@kshegunov
I just don't get it (I think I've said before that I get lost on all this Unicode etc. stuff).The OP has to put your code in his Qt app code. In his app code, who gets to know/choose whether
UNICODE_
is defined? He's going to useLPCTSTR
presumably to communicate with something external, and that's already compiled one way or the other. Sigh. -
@kshegunov
I just don't get it (I think I've said before that I get lost on all this Unicode etc. stuff).The OP has to put your code in his Qt app code. In his app code, who gets to know/choose whether
UNICODE_
is defined? He's going to useLPCTSTR
presumably to communicate with something external, and that's already compiled one way or the other. Sigh.@JonB said in Can't convert from QString to LPCTSTR:
I just don't get it (I think I've said before that I get lost on all this Unicode etc. stuff).
Nobody gets the windows API! ;)
Here he wants to convert a Qt type to a WinAPI type to (presumably) pass it on to some function in the WinAPI. The macro is defined in one of the system headers (I thinkwindows.h
from the SDK) and depends on how the windows was built/the compiler used. Granted any new windows (i.e. 95+) should already have that defined and you'd always haveTCHAR
=wchar_t *
, but nobody can grasp the depths of the dark forest the WinAPI is. In any case, he needs to match what windows, or rather the function he intents to use, expects.Take a look here as well. It should shed some light. Windows just uses macros for defining
ApiFunctionName
to be eitherApiFunctionNameA
(if using ASCII) orApiFunctionNameW
if using utf16. Wreaks all kinds of havoc when you need to actually use it. -
@JonB said in Can't convert from QString to LPCTSTR:
I just don't get it (I think I've said before that I get lost on all this Unicode etc. stuff).
Nobody gets the windows API! ;)
Here he wants to convert a Qt type to a WinAPI type to (presumably) pass it on to some function in the WinAPI. The macro is defined in one of the system headers (I thinkwindows.h
from the SDK) and depends on how the windows was built/the compiler used. Granted any new windows (i.e. 95+) should already have that defined and you'd always haveTCHAR
=wchar_t *
, but nobody can grasp the depths of the dark forest the WinAPI is. In any case, he needs to match what windows, or rather the function he intents to use, expects.Take a look here as well. It should shed some light. Windows just uses macros for defining
ApiFunctionName
to be eitherApiFunctionNameA
(if using ASCII) orApiFunctionNameW
if using utf16. Wreaks all kinds of havoc when you need to actually use it.depends on how the windows was built. Granted any new windows (i.e. 95+) should already have that defined and you'd always have
TCHAR = wchar_t *
Ah, to do with how Windows was built last century, OK. Yep I get that.
In that case, in the name of readability, why, my friend, do you not automatically suggest:
#ifdef UNICODE LPCWSTR something = someString.toStdWstring().c_str(); #else LPCSTR something = someString.toStdString().c_str(); #endif
instead of the scary-looking ones you're choosing?
-
depends on how the windows was built. Granted any new windows (i.e. 95+) should already have that defined and you'd always have
TCHAR = wchar_t *
Ah, to do with how Windows was built last century, OK. Yep I get that.
In that case, in the name of readability, why, my friend, do you not automatically suggest:
#ifdef UNICODE LPCWSTR something = someString.toStdWstring().c_str(); #else LPCSTR something = someString.toStdString().c_str(); #endif
instead of the scary-looking ones you're choosing?
@JonB said in Can't convert from QString to LPCTSTR:
In that case, in the name of readability, why, my friend, do you not automatically suggest
Returning pointers to temporaries is rather dangerous ... ;)
-
@JonB said in Can't convert from QString to LPCTSTR:
In that case, in the name of readability, why, my friend, do you not automatically suggest
Returning pointers to temporaries is rather dangerous ... ;)
@kshegunov
Damn it! OK depends on context, got it! Yours still looks ugly though :) -
@kshegunov
Damn it! OK depends on context, got it! Yours still looks ugly though :)@JonB said in Can't convert from QString to LPCTSTR:
Yours still looks ugly though
Through no fault of mine, complain to the WinAPI designers. :)
-
UPDATE:
how to convert to opposite now?))
From LPCTSTR to QString, i tried
QString::fromUtf16(lpctVAR)
But it dont work...
@Engelard said in Can't convert from QString to LPCTSTR:
how to convert to opposite now?
Same idea, either use
QString::fromUtf16
orQString::fromLatin1
depending on the macro. -
@Engelard said in Can't convert from QString to LPCTSTR:
how to convert to opposite now?
Same idea, either use
QString::fromUtf16
orQString::fromLatin1
depending on the macro.@kshegunov as i said, fromUtf16 not working(beneath screen of error), and latin1 also, it was before utf
-
@kshegunov as i said, fromUtf16 not working(beneath screen of error), and latin1 also, it was before utf