When I add an emoji to a button label, the app silently crashes when I run it.
I use Qt Creator v17.0 to create the layout of my app. I'm working on Python, using PySide6 (v6.9.1 on Python 3.13.2) and run it on Windows 11. I translate my .ui file into python code with pyside6-uic.
I debugged my app and tracked the cause of the issue to the retranslateUI() method (in the autogenerated part of the code). On it, the label of the button is set with
self.pushButton.setText(QCoreApplication.translate("EmojiButton", u"Click me \ud83d\ude80", None))
I think the error comes from QCoreApplication.translate(), as it seems it can't handle the escaped sequences that Qt Creator generates.
If I change the code into
self.pushButton.setText(QCoreApplication.translate("EmojiButton", u"Click me 🚀", None))
then the code works. Interestingly, if I use the 32bit escaped sequence of the emoji
self.pushButton.setText(QCoreApplication.translate("EmojiButton", u"Click me \U0001f680", None))
It works too! Sadly, that's not a viable solution, as this code is in the autogenerated part of the code and would be overwritten in the next change of the UI.
In the meantime, I avoid the issue by marking the button as not translatable.
I don't know if the bug is in PySide (as in, it should be able to parse the 16bit escape sequences) or in Qt Creator (it doesn't create the correct escape sequences). So feel free to move it if you think it's not in the correct section
I attach a minimal app to replicate the issue
UnicodeEncodeError: 'utf-8' codec can't encode characters in position 9-10: surrogates not allowed
Python:
u"Click me \ud83d\ude80"
C++:
"Click me \360\237\232\200"
For Gerrit Dashboard: PYSIDE-3173 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
675705,1 | uic/Python: Handle surrogates correctly | tqtc/lts-6.8 | qt/tqtc-qtbase | Status: NEW | 0 | 0 |