Search code examples
c++qtqt5

Unable to convert QString to WChar Array


QString processName = "test.exe";
QString::toWCharArray(processName);

I'm getting the following error:

error: C2664: 'QString::toWCharArray' : cannot convert parameter 1 from 'QString' to 'wchar_t *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Solution

  • You're using it incorrectly. You should be calling toWCharArray on the QString you want to convert and passing it a pointer to the first element of an array you have allocated:

    wchar_t array[9];
    QString processName = "test.exe";
    processName.toWCharArray(array);
    

    This fills array with the contents of processName.