const wchar_t* and QString Problem

  • I use an external DLL where I have to pass a parameter file.lpszSourceFilePath (const wchar_t*). If I use
    file.lpszSourceFilePath = L"C:\Users\ogni\Documents\video\early.ac3";
    then everything works well.
    But if I use a QString (from Filedialog) the parameter I pass seems to be wrong. Looks like a complete filename (_instead of /)
    I tried different way, also like to convert it with

    wchar_t* convertToFoxValue(QString strValue)
        wchar_t *pass;
        pass = (wchar_t*) malloc (sizeof(wchar_t)*strValue.size()+1);
        //pass = new wchar_t[strValue.size() + 1];
        //pass[strValue.length()]=0; // Null terminate the string
        return pass;

    But also this do not work. Cast the QString to utf16() also do not work. Just the plain text with L"....:" works, no variable.

    Anyone have a idea what there is wrong?

  • Windows uses backslash (\) as separator. Qt uses the unix separator (/)
    Have a try with:
    [static] QString QDir::toNativeSeparators(const QString &pathName)

  • Lifetime Qt Champion


    In addition to what mpergand suggested, if you only need a read-only access to the path (like const wchar_t * suggests), you can use QString::utf16() and a cast to wchar_t and therefore avoid the additional allocation and copy. Please note, that this is a Windows-only thing, as wchar_t can be 4 byte on Unix.


Log in to reply