Results 1 to 3 of 3

Thread: NSString to QString

  1. #1
    Join Date
    Aug 2009
    Posts
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default NSString to QString

    How can i convert NSString to QString? Or, better NSUrl to QUrl or QString
    I found inline function qt_mac_NSStringToQString, but i can't use it because headers are private and not installed on Mac by default. Besides, they use inncludes in form of "header.h" (#include <qapplication.h>) which are not seen to compiler too.

  2. #2
    Join Date
    Aug 2009
    Posts
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: NSString to QString

    Safe variant:
    Qt Code:
    1. QString qt_mac_NSStringToQString(const NSString *nsstr)
    2. {
    3. NSRange range;
    4. range.location = 0;
    5. range.length = [nsstr length];
    6. QString result(range.length, QChar(0));
    7.  
    8. unichar *chars = new unichar[range.location];
    9. [nsstr getCharacters:chars range:range];
    10. QString result = QString::fromUtf16(chars, range.length);
    11. delete chars;
    12. return result;
    13. }
    To copy to clipboard, switch view to plain text mode 

    Unsafe variant (a little faster):
    Qt Code:
    1. QString qt_mac_NSStringToQString(const NSString *nsstr)
    2. {
    3. NSRange range;
    4. range.location = 0;
    5. range.length = [nsstr length];
    6. QString result(range.length, QChar(0));
    7.  
    8. unichar *chars = reinterpret_cast<unichar *>(result.data())
    9. [nsstr getCharacters:chars range:range];
    10. return result;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Any suggestions?
    Last edited by ABBAPOH; 5th October 2010 at 11:00.

  3. #3
    Join Date
    Nov 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: NSString to QString

    Qt Code:
    1. QString qt_mac_NSStringToQString(const NSString *nsstr)
    2. {
    3. NSRange range;
    4. range.location = 0;
    5. range.length = [nsstr length];
    6. QString result(range.length, QChar(0));
    7.  
    8. unichar *chars = new unichar[range.length];
    9. [nsstr getCharacters:chars range:range];
    10. QString result = QString::fromUtf16(chars, range.length);
    11. delete[] chars;
    12. return result;
    13. }
    To copy to clipboard, switch view to plain text mode 

    works better

Similar Threads

  1. With QString create a QString&
    By avis_phoenix in forum Newbie
    Replies: 1
    Last Post: 21st April 2010, 22:05
  2. Replies: 4
    Last Post: 1st February 2010, 14:21
  3. QString arg
    By PLUS in forum Qt Programming
    Replies: 7
    Last Post: 30th January 2010, 10:59
  4. Replies: 4
    Last Post: 31st January 2008, 20:44
  5. how to copy part of QString to anothe QString
    By nass in forum Qt Programming
    Replies: 1
    Last Post: 26th March 2007, 19:05

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.