Results 1 to 4 of 4

Thread: QString::fromUtf8 problems converting micro sign

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy QString::fromUtf8 problems converting micro sign

    I am trying to convert the unicode micro-sign to use in a QString. I have tried the following:
    Qt Code:
    1. QByteArray b(1, 0xb5);
    2. QString s = QString::fromUtf8(b.constData(), b.length());
    To copy to clipboard, switch view to plain text mode 
    But the string isn't the micro-sign but a question mark in a circle.
    I also tried:
    Qt Code:
    1. QChar c(181);
    2. QString s(c);
    To copy to clipboard, switch view to plain text mode 
    With the same result.
    When using this with PyQt:
    Qt Code:
    1. b = QByteArray(1, "\xb5")
    2. s = QString.fromUtf8(b.data(), b.length())
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. c = QChar(181);
    2. s = QString(c);
    To copy to clipboard, switch view to plain text mode 
    both c and b when I print them are also the question mark in the circle. When I try to convert to a QString I get the following error from the python interpreter:
    Qt Code:
    1. UnicodeEncodeError: 'ascii' codec can't encode character u'\xb5' in position 0:ordinal not in range(128)
    To copy to clipboard, switch view to plain text mode 
    I have tried a variety of combinations and googled everywhere for an adequate solution, to no avail.

    FWIW, the python string conversion works as I'd expect, but I need a QString:
    Qt Code:
    1. >> s = unichr(181).encode("utf8")
    2. >> print s
    3. >> µ
    4. >> print QString(s)
    5. Traceback (most recent call last):
    6. File "<input>", line 2, in <module>
    7. UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
    To copy to clipboard, switch view to plain text mode 
    Any help with this is greatly appreciated.
    Last edited by smacchia; 8th February 2011 at 23:15.

Similar Threads

  1. converting uint to QString
    By Yayati.Ekbote in forum Newbie
    Replies: 1
    Last Post: 14th April 2010, 13:43
  2. Converting QString to char* in onl line
    By hubbobubbo in forum Qt Programming
    Replies: 10
    Last Post: 11th December 2009, 11:45
  3. Converting a QString to a LPCTSTR?
    By dobedidoo in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2009, 14:27
  4. Converting u_char to QString
    By merlvingian in forum Newbie
    Replies: 7
    Last Post: 29th September 2006, 00:11
  5. QString to char* not converting
    By DPinLV in forum Qt Programming
    Replies: 17
    Last Post: 6th August 2006, 12:15

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
  •  
Qt is a trademark of The Qt Company.