1

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?

10
  • Hmmm, what if you tried to use a char hexcode? Like chr(0x1F923) Commented Aug 27 at 16:36
  • PyQt6 shows UnicodeEncodeError: 'utf-8' codec can't encode characters in position 9-10: surrogates not allowed and when I replace \u with upper \U then SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 9-14: truncated \UXXXXXXXX escape Commented Aug 27 at 16:46
  • even normal print( "click me \ud83e\udd23\ufe0f" ) raise the same error. Maybe it is wrong value. Commented Aug 27 at 16:51
  • What Qt version are you using? Also, ensure that you run the code from a prompt, don't just use the IDE. Commented Aug 27 at 16:59
  • 1
    @furas It's possible that newer Qt, PySide or even Python versions did fix the issue, but the specific bug wasn't updated since then (I've seen some of them). Also, Qt6.9 introduced changes in font behavior, specifically addressing Unicode/Emoji aspects, and releases in the same or higher minor Qt version addressed that aspect as well. This seems like something that has to be thoroughly checked across different OS/Qt/binding combinations due to font rendering and byte/unicode/character related aspects. Commented Aug 29 at 2:46

1 Answer 1

1

Edit: It was a bug in PySide for versions 6.9.2 and earlier and it's fixed from v6.9.3. The code above now works.

Original Answer: It seems that is a bug, either in Qt Creator (not generating the correct escaped sequence) or in PySide (pyside6-uic doesn't generate the correct escaped sequence for QCoreApplication.translate() or QCoreApplication.translate() doesn't accept 16bit escape sequences).

A bug that seems to be related (QTBUG-122975, as pointed by @musicamante in the discussion) seems to be open since 2024.

As a workaround, for the time being, if your app doesn't need translation, you can deselect the translatable property in the QAbstractButton properties.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.