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

我收到以下错误:

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
4

4 回答 4

11

你使用不正确。您应该调用要转换的对象并将其传递给您已分配的数组的第一个元素的指针toWCharArrayQString

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

这填充arrayprocessName.

于 2013-04-25T22:32:37.947 回答
9

我发现当前的答案还不够,“数组”可能包含未知字符,因为“数组”没有零终止。

我的应用程序中有这个错误,并花了很长时间才弄清楚。

更好的方法应该是这样的:

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;
于 2017-02-21T07:26:51.923 回答
2

1行整洁的解决方案:

processName.toStdWString().c_str()
于 2019-07-03T12:57:51.757 回答
0

我使用了 Jake W 的答案。他使用的是 toWCharArray 方法。不幸的是,这种方法不会终止字符串,这就是它在我的情况下不起作用的原因。这个工作完美:

QString processName = "test.exe";
(wchar_t*)processName.utf16();
于 2020-11-30T22:21:04.507 回答