Unicode is tricky, it bites me every time. This works:

########################
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui

quotationMark = u'\u201c'
myText = u'special charactes cause headaches: {0}'.format(quotationMark)
print myText  # this prints the crrect special character (quotation mark)

app = QtGui.QApplication([])
w = QtGui.QWidget()
w.setToolTip(myText)  # this just ends up displaying a "å" character
w.show()
w.raise_()
sys.exit(app.exec_())
#########################


Please note, the .py file actually has to be saved/encoded as utf-8. You need to do this via your editor's save/convert function.


On 03/02/2016 07:20 AM, Frank Rueter | OHUfx wrote:
Hi guys,

I've been reading up on unicode and character encoding a bit today and just when I thought I had a vague idea how it all works I ran into this problem:

I am retrieving unicode from a website, which I need to format a little, then display as a widget's tooltip. When I print out my formatted string I get the special characters I expect, but when I use the same string as a tooltip, it displays a "å" character instead.
Here is a screen shot of what I'm seeing:


And below is a simple test that shows my problem.
Can somebody please explain to me what is going on here and how I should be handle this properly?

Cheers,
frank

    import sys
    from PySide import QtGui

    quotationMark = u'\u201c'
    myText = 'special charactes cause headaches:
    {}'.format(quotationMark.encode('utf-8'))
    print myText # this prints the crrect special character (quotation
    mark)

    app = QtGui.QApplication([])
    w = QtGui.QWidget()
    w.setToolTip(myText) # this just ends up displaying a "å" character
    w.show()
    w.raise_()
    sys.exit(app.exec_())


--
ohufxLogo 50x50 <http://www.ohufx.com> *vfx compositing <http://ohufx.com/index.php/vfx-compositing> | *workflow customisation and consulting <http://ohufx.com/index.php/vfx-customising>* *



_______________________________________________
PySide mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/pyside

--

_*check out pointcloud9.com*_ <http://pointcloud9.com/>

**Sebastian Elsner - Pipeline Technical Director - RISE*

t: +49 30 20180300 [email protected] _ <mailto:[email protected]>
f: +49 30 61651074 _www.risefx.com_ <http://www.risefx.com/>*

*RISE FX GmbH*
*Schlesische Straße 28, 10997 Berlin
An der Schanz 1A, 50735 Köln
Büchsenstraße 20, 70174 Stuttgart
Gumpendorferstraße 55, 1060 Wien
Geschaeftsfuehrer: Sven Pannicke, Robert Pinnow
Handelsregister Berlin HRB 106667 B*

_______________________________________________
PySide mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/pyside

Reply via email to