Results 1 to 8 of 8

Thread: QString to Unicode to char*

  1. #1
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question QString to Unicode to char*

    Hi all,
    I want to be able to convert my QString in unicode format and the convert it in char*.

    I have try with the following code but I have a casting problem (my QByteArray contains only the first character).

    Qt Code:
    1. QByteArray xByteArray((const char *)qsTmp.unicode(), qsTmp.size()*2);
    To copy to clipboard, switch view to plain text mode 

    Any Suggestions?

    with regards, Dax

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QString to Unicode to char*

    Let's see:

    Quote Originally Posted by the documentation
    The QString class provides a Unicode character string
    That's already taken care of.

    Now, to turn it into a char pointer:
    Qt Code:
    1. QString myString("Hello world");
    2.  
    3. char *myStringChars = myString.toUtf8().data();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString to Unicode to char*

    Thankyou tbscope,
    are you sure that this code work correctly? My debugger (VS2005) show me bad characters in myStringChars but I don't know if is a format-code problem.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QString to Unicode to char*

    You mean you actually have a unicode string?
    Yes, that's correct.

    Understand what a unicode string is.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QString to Unicode to char*

    Quote Originally Posted by tbscope View Post
    Now, to turn it into a char pointer:
    Qt Code:
    1. QString myString("Hello world");
    2. char *myStringChars = myString.toUtf8().data();
    To copy to clipboard, switch view to plain text mode 
    That's likely to end in tears. QString::toUtf8() returns a temporary QByteArray, data() returns a pointer to its data buffer, and then the temporary QByteArray ceases to exist. Ultimately, you get a char* to a buffer inside a temporary object that no longer exists and can therefore be overwritten by other machine processes.

    You need to control the scope of the QByteArray:
    Qt Code:
    1. QByteArray ba = myString.toUtf8();
    2. char *myStringChars = ba.data();
    3. // do stuff with myStringChars
    4. // let ba go out of scope
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QString to Unicode to char*

    Yes, now I remember it :-)
    I knew there was something wrong with it.

  7. #7
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString to Unicode to char*

    Ok, thanks

  8. #8
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded

    Default Re: QString to Unicode to char*

    Consider using the qPrintable macro.

Similar Threads

  1. Putting unicode in QString literals
    By reddish in forum Qt Programming
    Replies: 3
    Last Post: 30th October 2012, 01:25
  2. QString and Unicode
    By juanjo_de_la_pradera in forum Qt Programming
    Replies: 0
    Last Post: 23rd September 2010, 13:38
  3. QString unicode problem
    By kemp in forum Qt Programming
    Replies: 6
    Last Post: 21st September 2010, 15:11
  4. QString Unicode conversion
    By user_mail07 in forum Qt Programming
    Replies: 5
    Last Post: 15th April 2010, 22:16
  5. char* from QString
    By bruccutler in forum Newbie
    Replies: 16
    Last Post: 14th February 2007, 06:36

Tags for this Thread

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.