I am trying to convert the unicode micro-sign to use in a QString. I have tried the following:
QByteArray b(1, 0xb5);
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:
QChar c(181);
QString s(c);
To copy to clipboard, switch view to plain text mode
With the same result.
When using this with PyQt:
s
= QString.
fromUtf8(b.
data(), b.
length())
b = QByteArray(1, "\xb5")
s = QString.fromUtf8(b.data(), b.length())
To copy to clipboard, switch view to plain text mode
or
c = QChar(181);
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:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb5' in position 0:ordinal not in range(128)
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:
>> s = unichr(181).encode("utf8")
>> print s
>> µ
Traceback (most recent call last):
File "<input>", line 2, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
>> s = unichr(181).encode("utf8")
>> print s
>> µ
>> print QString(s)
Traceback (most recent call last):
File "<input>", line 2, in <module>
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.
Bookmarks