Results 1 to 6 of 6

Thread: Assign a wchar_t array to QString

  1. #1
    Join Date
    Apr 2009
    Posts
    15
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Assign a wchar_t array to QString

    I use QString::fromWCharArray(wchar_t*) to assign a wchar_t array to QString but the QString always is empty.

    Here is my code:
    Qt Code:
    1. wchar_t * temp = new wchar_t[4];
    2. temp[0] = 'a'; temp[1] = 'b'; temp[3] = 'c'; temp[4] = 0;
    3. QString test;
    4. test.fromWCharArray(temp);
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Assign a wchar_t array to QString

    First of all, QString::fromWCharArray() is a STATIC method which RETURNS the converted string:
    Qt Code:
    1. QString test = QString::fromWCharArray(temp);
    To copy to clipboard, switch view to plain text mode 
    Secondly, the initialization of your wchar array is wrong. It's not an array of chars but wide chars. Try something like this:
    Qt Code:
    1. wchar_t * temp = L"abc";
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Apr 2009
    Posts
    15
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Assign a wchar_t array to QString

    I tried the second but it doesn't help
    Qt Code:
    1. wchar_t * temp = L"abc";
    2. QString test;
    3. test.fromWCharArray(temp);
    To copy to clipboard, switch view to plain text mode 
    test = "" finally.

    Many thanks for your help anyway.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Assign a wchar_t array to QString

    I told you that QString::fromWCharArray() is a static method that RETURNS the string. Statement
    Qt Code:
    1. test.fromWCharArray(temp);
    To copy to clipboard, switch view to plain text mode 
    as not effect at all.
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    jacky (18th April 2009)

  6. #5
    Join Date
    Apr 2009
    Posts
    15
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Assign a wchar_t array to QString

    Many thanks for your help.

  7. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Assign a wchar_t array to QString

    Jacky, please read posts carefully. As jpn said, you have to do like this:
    Qt Code:
    1. QString test = QString::fromWCharArray(temp);
    To copy to clipboard, switch view to plain text mode 
    bacause QString::fromWCharArray() is a STATIC member function. Please check some C++ tutorial if you don't clearly understand what static members are.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. Qy 4.4.3 MySQL driver failed
    By pamalite in forum Installation and Deployment
    Replies: 2
    Last Post: 23rd January 2010, 01:09
  2. Char Array to QString
    By 3nc31 in forum Qt Programming
    Replies: 2
    Last Post: 25th November 2007, 22:18
  3. how to copy QString content into array
    By eric in forum Qt Programming
    Replies: 12
    Last Post: 14th November 2007, 23:13
  4. Problem in converting QString to QChar array?
    By KaKa in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2007, 00:38
  5. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.