Results 1 to 4 of 4

Thread: QString::fromUtf8 problems converting micro sign

  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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString::fromUtf8 problems converting micro sign

    0xb5 is very unlikely to be a micro. The best thing you can do is to just put the real micro character in the file and save it with utf-8 encoding. Also make sure your font contains a micro character at this position.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    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::fromUtf8 problems converting micro sign

    The Unicode code point for the "Micro sign" is \u00b5. In UTF-8 encoding this is a two byte sequence \xc2\xb5. You could also use Greek small letter mu \u03bc.

    Any of these:
    Qt Code:
    1. QString s(QChar(0x00b5));
    2. QString s = QString::fromStdWString(L"\u00b5");
    3. QString s = QString::fromUtf8("\xc2\xb5");
    To copy to clipboard, switch view to plain text mode 

    This works for me but I am not sure if it is by-design or by-accident:
    Qt Code:
    1. QString s = QString::fromUtf8("\u00b5");
    To copy to clipboard, switch view to plain text mode 

    Look up Unicode code points here: http://www.unicode.org/charts
    Convert into a bucket of other forms here: http://people.w3.org/rishida/tools/conversion/
    Last edited by ChrisW67; 10th February 2011 at 00:32.

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

    Default Re: QString::fromUtf8 problems converting micro sign

    Thanks! This is a big help.

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