Important: Please read the Qt Code of Conduct -

QString to TCHAR and TCHAR*

  • Hi all,
    I am using a dll, which takes a TCHAR and TCHAR*parameter in its functions.
    and my GUI elements from QT are in the form of QString.

    QT uses UTF-8 encoding which has (TCHAR as WCHAR)

    I have been working since hours to figure out a way to convert a QString value to TCHAR / TCHAR*
    have tried many forms and types..still no joy.
    please can i get a code example to convert a QString value to TCHAR / TCHAR*

    QString Value to TCHAR tValue
    and QString Value to TCHAR* rValue


  • TCHAR is just typedef of char if you use ANSI or wchar_t with UNICODE.
    if library needs ANSI:
    TCHAR tc = string[0].toAscii();
    TCHAR *tc = string.toAscii().data();

    UNICODE way:
    static HRESULT _C2W(const char * pszChar, wchar_t **ppszwChar)
    HRESULT hr = S_OK;
    unsigned int iRetVal = 0;
    iRetVal = MultiByteToWideChar(CP_ACP,0,pszChar,-1,NULL,0);
    if(iRetVal == 0){(*ppszwChar) = NULL; return E_FAIL;}
    *ppszwChar = (wchar_t *) new wchar_t[iRetVal];
    if((*ppszwChar) == NULL) return E_FAIL;
    iRetVal = MultiByteToWideChar(CP_ACP,0,pszChar,-1,*ppszwChar,iRetVal);
    return iRetVal;

    TCHAR * wc = NULL;
    TCHAR w = wc[0];

    QString has function toWCharArray, but it is bugy...

Log in to reply