I'm using Qt Creator to design the GUI for an app. I wanted to add some emoji to the button labels but the app silently crashes when I do. I followed the error to the retranslateUI() function (which is autogenerated code, when generating a py file from a ui one with pyside6-uic). On it, labels are set in my widgets. The autogenerated code uses escape sequences to represent my emoji, and QCoreApplication.translate seems to not accept it. If I replace the hex codes for the emoji, the app launches, but this is autogenerated code, so I can't edit it as the next time the original file changes, my changes will be overwritten.
I'm using PySide6 (v6.9.1) and Python 3.13 on Windows 11.
I managed to replicate the issue with this code snippet:
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton
from PySide6.QtCore import QCoreApplication
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(500, 500, 400, 200)
self.setWindowTitle("Button")
button = QPushButton(self)
# When I generate a `py` file from the `ui` one, `setupUI()` is called, which
# eventually calls `retranslateUI()`, which sets titles, labels, etc...
# When it's time to set the label for the button, the code autogenerated uses
# the UTF hex codes for the emoji, and the app crashes here
button.setText(QCoreApplication.translate("MainWindow", "click me \ud83e\udd23\ufe0f", None)) # this would crash the app
# But using emoji in the string seems to work fine
# button.setText(QCoreApplication.translate("MainWindow", "click me 🤣️", None)) # this works
button.setGeometry(100, 75, 200, 50)
button.clicked.connect(self.click)
self.show()
def click(self):
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = Window()
sys.exit(app.exec_())
I think it's an issue with QCoreApplication.translate(), as labelling the button with either emoji or escape sequences create buttons with emoji on them
# Both these options work, either using an emoji
# or the UTF code for the emoji, both create a button with an emoji on it
button1 = QPushButton("click me 🤣️", self) # this works
button2 = QPushButton("click me \ud83e\udd23\ufe0f", self) # this works too
Does anybody know how to get emoji as part of the label on a button widget?
chr(0x1F923)PyQt6showsUnicodeEncodeError: 'utf-8' codec can't encode characters in position 9-10: surrogates not allowedand when I replace\uwith upper\UthenSyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 9-14: truncated \UXXXXXXXX escapeprint( "click me \ud83e\udd23\ufe0f" )raise the same error. Maybe it is wrong value.