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?



  • @Engelard
    What about

    str.toStdString().c_str();
    

    google all say that must be .toUtf16(); but i dont have that

    QString does not have .toUtf16(). It does have .utf16(). Which would work. But will require a cast. Which people don't like these days... :)


  • Qt Champions 2017

    LPCTSTR is either const char * or const wchar_t * or const 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 use LPCTSTR presumably to communicate with something external, and that's already compiled one way or the other. Sigh.


  • Qt Champions 2017

    @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 think windows.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 have TCHAR = 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 either ApiFunctionNameA (if using ASCII) or ApiFunctionNameW if using utf16. Wreaks all kinds of havoc when you need to actually use it.



  • @kshegunov

    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?


  • Qt Champions 2017

    @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 :)


  • Qt Champions 2017

    @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...


  • Qt Champions 2017

    @Engelard said in Can't convert from QString to LPCTSTR:

    how to convert to opposite now?

    Same idea, either use QString::fromUtf16 or QString::fromLatin1 depending on the macro.



  • @kshegunov as i said, fromUtf16 not working(beneath screen of error), and latin1 also, it was before utf

    alt text



  • @Engelard well theres all the information you need, pass the length of the LPCSTR as 2nd parameter of fromUtf16


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.