PDA

View Full Version : Assign a wchar_t array to QString



jacky
18th April 2009, 07:25
I use QString::fromWCharArray(wchar_t*) to assign a wchar_t array to QString but the QString always is empty.

Here is my code:


wchar_t * temp = new wchar_t[4];
temp[0] = 'a'; temp[1] = 'b'; temp[3] = 'c'; temp[4] = 0;
QString test;
test.fromWCharArray(temp);

The result is test = "". My expectation is test = "abc".

I appreciate very much if anyone could tell me what's wrong.
Many thanks in advance.

jpn
18th April 2009, 08:44
First of all, QString::fromWCharArray() is a STATIC method which RETURNS the converted string:


QString test = QString::fromWCharArray(temp);

Secondly, the initialization of your wchar array is wrong. It's not an array of chars but wide chars. Try something like this:


wchar_t * temp = L"abc";

jacky
18th April 2009, 10:26
I tried the second but it doesn't help


wchar_t * temp = L"abc";
QString test;
test.fromWCharArray(temp);

test = "" finally.

Many thanks for your help anyway.

jpn
18th April 2009, 11:44
I told you that QString::fromWCharArray() is a static method that RETURNS the string. Statement

test.fromWCharArray(temp);
as not effect at all.

jacky
18th April 2009, 12:24
Many thanks for your help.

faldzip
18th April 2009, 12:28
Jacky, please read posts carefully. As jpn said, you have to do like this:


QString test = QString::fromWCharArray(temp);
bacause QString::fromWCharArray() is a STATIC member function. Please check some C++ tutorial if you don't clearly understand what static members are.