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
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
.
I found the current answer is not enough, 'array' may contain unknown characters because no zero termination to 'array'.
I had this bug in my app and spent a long time to figure it out.
A better way should be looking like this:
QString processName = "test.exe";
wchar_t *array = new wchar_t[processName.length() + 1];
processName.toWCharArray(array);
array[processName.length()] = 0;
// Now 'array' is ready to use
... ...
// then delete in destructor
delete[] array;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With