PDA

View Full Version : Change QString to LPWSTR ?



nackasha
3rd August 2011, 17:34
Hello,

I have a LineEdit, i want to retrieve the string and pass it to winapi.

WinApi input type is LPWSTR, and LineEdit type is QString. May kindly advise me on how to change from QString to LPWSTR.

Thank you

stampede
3rd August 2011, 19:01
Use QString::toStdWString (http://doc.qt.nokia.com/latest/qstring.html#toStdWString):


// not tested
QString string = ...;
std::wstring wstr = string.toStdWString();
LPWSTR ptr = wstr.c_str();

nackasha
3rd August 2011, 19:31
Please click one of the Quick Reply icons in the posts above to activate Quick Reply.

Sorry:

It is giving me the error "invalid conversion from 'const wchar_t*' to 'WCHAR*'" :confused:

Thank you

Added after 23 minutes:

I solved it this way :

QString passQt;
wchar_t* pass;
pass = (wchar_t*) malloc (sizeof(wchar_t)*passQt.length()+1);
passValQt.toWCharArray(pass);
pass[passQt.length()]=0; // Null terminate the string

It gives the correct result. What do you think about the solution ?

Thank you

stampede
3rd August 2011, 19:37
Ok, so try with LPCWSTR (C means const) instead LPWSTR in conversion. If the winapi method requires pointer to wide string (not pointer to const wide string), you will need to allocate the memory for wstring yourself and copy the wstring.c_str() content ( you should not modify the pointer returned by c_str() ).
Or maybe you can use QString::toWCharArray with self-allocated wchar_t array.
---
edit:
Thats what I meant in second part of this post, but maybe this way:


int length = passValQt.toWCharArray(pass);
pass[length]=0; // Null terminate the string