使用 QFontDatabase.addApplicationFont() 时出现 0xC0000005

问题描述 投票:0回答:1

我在使用 PyQt6(版本 6.5.2)时使用以下代码:

self.font_id = QFontDatabase.addApplicationFont('conf/font.otf')
self.font_family = QFontDatabase.applicationFontFamilies(font_id)

for name, classtype in self.__dict__.items():
    try:
        classtype.setFont(QFont(self.font_family[0], int(self.base_cfg['Config']['fontSize']), QFont.Weight.Bold))
    except AttributeError:
        pass

无论我在 PyCharm 中运行或调试程序,我总是收到消息“进程已完成,退出代码 -1073741819 (0xC0000005)”。但是当我注释掉前两行代码时,程序运行正常。

经过多次尝试,我现在确信 QFontDatabase.addApplicationFont('conf/font.otf') 本身对我来说是最小的可重现示例。即使我只执行这两行,脚本也会崩溃:

from PyQt6.QtGui import QFontDatabase
QFontDatabase.addApplicationFont('conf/font.otf')

我已经在Python 3.9.4/3.10.2/3.11.4上对此进行了测试,情况仍然相同。我使用的是 Windows 11 版本 22621.2070。我搜索了相关信息,但还没有找到任何有用的信息。

我尝试了 PyCharm 运行/调试配置中的 -X dev 解释器选项,我得到了这样的结果:

Windows fatal exception: access violation

Thread 0x00002a00 (most recent call first):
  File "D:\Software\PyCharm Community Edition 2023.2\plugins\python-ce\helpers\pydev\pydevd.py", line 138 in _on_run
  File "D:\Software\PyCharm Community Edition 2023.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 219 in run
  File "D:\Software\Python\lib\threading.py", line 1009 in _bootstrap_inner
  File "D:\Software\Python\lib\threading.py", line 966 in _bootstrap

Thread 0x00004cd4 (most recent call first):
  File "D:\Software\PyCharm Community Edition 2023.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 293 in _on_run
  File "D:\Software\PyCharm Community Edition 2023.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 219 in run
  File "D:\Software\Python\lib\threading.py", line 1009 in _bootstrap_inner
  File "D:\Software\Python\lib\threading.py", line 966 in _bootstrap

Thread 0x00004728 (most recent call first):
  File "D:\Software\Python\lib\threading.py", line 324 in wait
  File "D:\Software\Python\lib\queue.py", line 180 in get
  File "D:\Software\PyCharm Community Edition 2023.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 368 in _on_run
  File "D:\Software\PyCharm Community Edition 2023.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 219 in run
  File "D:\Software\Python\lib\threading.py", line 1009 in _bootstrap_inner
  File "D:\Software\Python\lib\threading.py", line 966 in _bootstrap

有人可以帮我吗?

python fonts pycharm pyqt6
1个回答
0
投票

感谢@musicamante的提醒:

顾名思义,

addApplicationFont()
向应用程序添加字体,但您根本没有创建任何应用程序。至少在调用 addApplicationFont()之前
创建一个 QApplication 实例。

我确实在这件事上犯了一个错误。问题是这样发生的:

我的应用程序窗口的结构

gui.py

如下所示:

from PyQt6.XXX import XXX class MyMainWindow(QMainWindow): font_id = QFontDatabase.addApplicationFont('conf/font.otf') font_family = QFontDatabase.applicationFontFamilies(font_id) def __init__(self): # some init process def main(): app = QApplication(sys.argv) _ = MyMainWindow() sys.exit(app.exec()) if __name__ == '__main': main()
并且,在我的应用程序的主入口:

import gui gui.main()
整个过程中,

addApplicationFont()

是在
之前app = QApplication(sys.argv)
执行的。将这两行移入
__init__()
部分后一切正常:

class MyMainWindow(QMainWindow): def __init__(self): # some init process self.font_id = QFontDatabase.addApplicationFont('conf/font.otf') self.font_family = QFontDatabase.applicationFontFamilies(font_id)
    
© www.soinside.com 2019 - 2024. All rights reserved.