Python Forum
[PyQt] QActions and Unicode glyphs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] QActions and Unicode glyphs
#1
I'm trying to add Unicode glyphs to a QAction. But, my attempts thus far are crashing my desktop (actually logging me out). How do I use Unicode characters in QActions?
I've tried directly in the IDE like so:
QAction("• my string")

I've tried via Python (3.10.5) escape sequences, like so:
QAction("\u0394 my string")

and finally, I've tried with QObject.tr(), like so:
str = QObject.tr("\u0394 my string")
QAction(str)
How do I pass unicode characters to a QAction? I don't want crashes, now-what-i-mean.
Reply
#2
Example

import sys

from PyQt5.QtCore import QSize, Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (
    QAction,
    QApplication,
    QCheckBox,
    QLabel,
    QMainWindow,
    QStatusBar,
    QToolBar,
)

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("My App")

        label = QLabel("Hello!")
        label.setAlignment(Qt.AlignCenter)

        self.setCentralWidget(label)

        toolbar = QToolBar("My main toolbar")
        toolbar.setIconSize(QSize(16, 16))
        self.addToolBar(toolbar)

        button_action = QAction("\u0394 my string", self)
        button_action.setStatusTip("This is a button with glyphs")
        button_action.triggered.connect(self.onMyToolBarButtonClick)
        toolbar.addAction(button_action)

        self.setStatusBar(QStatusBar(self))

    def onMyToolBarButtonClick(self, s):
        print("click", s)


app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec()
Reply
#3
Thanks, but I've tried that. You can see my question above and what I've tried that all cause the PC to crash/log out.

I just tested my app without a Unicode glyph and it seems stable. With what you've posted (that I've tried), the glyph prints, but it causes my system to log out.
Also, I should have shared this, and I don't know if it affects anything, but my stings are formatted like so:
-without Unicode (no crashes):
QAction(f"- this is a string {bytes.decode()}")

-with Unicode (crashes):
Q(f"• this is a string {bytes.decode()}")

I don't know if the decoding is making a difference...
Reply
#4
This works for me. I increased font size to better see the glyph. I used UTF-16

import sys

from PyQt5.QtCore import QSize, Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (
    QAction,
    QApplication,
    QCheckBox,
    QLabel,
    QMainWindow,
    QStatusBar,
    QToolBar,
)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("My App")

        label = QLabel("Hello!")
        label.setAlignment(Qt.AlignCenter)

        self.setCentralWidget(label)

        toolbar = QToolBar("My main toolbar")
        toolbar.setIconSize(QSize(16, 16))
        toolbar.setStyleSheet('font-size: 35px;')
        self.addToolBar(toolbar)

        button_action = QAction("\U0001F9DD my string", self)
        button_action.setStatusTip("This is a button with glyphs")
        button_action.triggered.connect(self.onMyToolBarButtonClick)
        toolbar.addAction(button_action)

        self.setStatusBar(QStatusBar(self))

    def onMyToolBarButtonClick(self, s):
        print("click", s)


app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec()

Attached Files

Thumbnail(s)
   
I welcome all feedback.
The only dumb question, is one that doesn't get asked.

My Scripts
CookBook - Shmup - PyQt5 Music Player


Reply
#5
The problem isn't getting Unicode characters to show, I have no trouble with that. The problem is when I use one, it causes my Linux Fedora (if that makes a difference) based PC to log me out of the desktop.
Reply
#6
Have you tried running your script in the terminal?
Reply
#7
Nevermind. Sorry guys. After removing the Unicode glyphs and quite a while of stability, I just got a crash. So, I have no idea what the problem is, but it still crashes. I was getting frequent crashes with glyphs, but after a while of stability, without glyphs it still crashes.

I hope you can see why I thought it was the glyphs. Bad for me because I have no clue what's causing it. Python may print an error to the console, but it crashes, so I can't see it. It could be QWebEngineView and my OS... but I have no reason to believe that, 'cause I have no idea why it crashes. The OS error is
Additional Information:
Source Context system_u:system_r:abrt_t:s0-s0:c0.c1023
Target Context system_u:object_r:xserver_misc_device_t:s0
Target Objects /dev/nvidiactl [ chr_file ]
Source gdb
Source Path gdb

and more. "chr_file"? Time to search engine.
Reply
#8
Sorry to spam the thread, but I think I found the issue for anyone interested. It isn't Unicode at all (so it appears after 7 hours of stress testing), yet a malformed call to QVBoxLayout(). I called the method addWidget() like so:
box.addWidget(widget, Qt.AlignmentFlag.AlignTop)

The alignment flag argument was placed where the "stretch" argument should go. I should have done:
box.addWidget(widget, alignment=Qt.AlignmentFlag.AlignTop)

It's been stable for 7 hours, so if that is in fact the cause of the crashes, I'm surprised something like that would cause such instability. I mean why wouldn't it just crash the app? Or not run in the first place? Hopefully those questions don't indicate that I didn't find the issue. So far, so good.

Sorry for the bad question to begin with.
Reply
#9
You didn't mention that you use PyQt6.
PyQt6 is not as forgiving of errors as PyQt5, and tends to crash immediately with no error message.
Reply
#10
Yeah, @Axel_Erfurt, I'm running PyQt6 v6.4.0. It may be a fallacy, but I sorta think like the latest is the greatest--new features, bug fixes from past versions, etc. I did not know it isn't as forgiving of errors. I found that out the hard way (*fingers crossed* I found the bug).
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020