Results 1 to 4 of 4

Thread: Change QString to LPWSTR ?

  1. #1
    Join Date
    Jul 2011
    Posts
    16
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Change QString to LPWSTR ?

    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

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Change QString to LPWSTR ?

    Use QString::toStdWString:
    Qt Code:
    1. // not tested
    2. QString string = ...;
    3. std::wstring wstr = string.toStdWString();
    4. LPWSTR ptr = wstr.c_str();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2011
    Posts
    16
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change QString to LPWSTR ?

    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*'"

    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
    Last edited by nackasha; 3rd August 2011 at 19:31.

  4. The following user says thank you to nackasha for this useful post:

    KalEl (9th May 2017)

  5. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Change QString to LPWSTR ?

    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:
    Qt Code:
    1. int length = passValQt.toWCharArray(pass);
    2. pass[length]=0; // Null terminate the string
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to stampede for this useful post:

    nackasha (3rd August 2011)

Similar Threads

  1. Replies: 8
    Last Post: 25th November 2010, 11:40
  2. Replies: 4
    Last Post: 1st February 2010, 14:21
  3. Replies: 4
    Last Post: 31st January 2008, 20:44
  4. cannot convert parameter 2 from 'char [80]' to 'LPWSTR'
    By sabeesh in forum Qt Programming
    Replies: 5
    Last Post: 4th December 2007, 14:21
  5. Change colour of QString
    By Morea in forum Newbie
    Replies: 9
    Last Post: 10th February 2006, 22:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.