Posts: 131
Threads: 35
Joined: Apr 2018
Oct-05-2022, 12:38 PM
(This post was last modified: Oct-05-2022, 12:39 PM by malonn.)
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.
Posts: 1,028
Threads: 16
Joined: Dec 2016
Oct-05-2022, 12:54 PM
(This post was last modified: Oct-05-2022, 12:54 PM by Axel_Erfurt.)
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()
Posts: 131
Threads: 35
Joined: Apr 2018
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...
Posts: 1,144
Threads: 114
Joined: Sep 2019
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()
Posts: 131
Threads: 35
Joined: Apr 2018
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.
Posts: 1,028
Threads: 16
Joined: Dec 2016
Have you tried running your script in the terminal?
Posts: 131
Threads: 35
Joined: Apr 2018
Oct-05-2022, 11:38 PM
(This post was last modified: Oct-05-2022, 11:38 PM by malonn.)
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.
Posts: 131
Threads: 35
Joined: Apr 2018
Oct-06-2022, 06:58 PM
(This post was last modified: Oct-06-2022, 07:00 PM by malonn.)
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.
Posts: 1,028
Threads: 16
Joined: Dec 2016
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.
Posts: 131
Threads: 35
Joined: Apr 2018
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).
|