Skip to content

Commit 3aedb19

Browse files
committed
修复字体设置控制台PointSize(-1)警告
1 parent f5a7e8f commit 3aedb19

4 files changed

Lines changed: 62 additions & 3 deletions

File tree

CHANGELOG/v2.3.0-beta.1/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ v2.3 - Shirako (砂狼白子) beta 1
2525
- 修复 **Linux通知** 缺少notify-send崩溃
2626
- 修复 **Linux浮窗** 无焦点激活异常
2727
- 修复 **Linux浮窗** 拖动失效
28+
- 修复 **字体设置** 控制台PointSize(-1)警告
2829

2930
## 🔧 其它变更
3031

app/core/app_init.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
from app.tools.settings_access import readme_settings_async
77
from app.tools.update_utils import check_for_updates_on_startup
88
from app.tools.variable import APP_INIT_DELAY
9-
from app.core.font_manager import apply_font_settings
9+
from app.core.font_manager import (
10+
apply_font_settings,
11+
ensure_application_font_point_size,
12+
)
1013
from app.core.window_manager import WindowManager
1114
from app.core.utils import safe_execute
1215

@@ -59,6 +62,7 @@ def _apply_theme(self) -> None:
5962
setTheme(Theme.AUTO)
6063
else:
6164
setTheme(Theme.LIGHT)
65+
ensure_application_font_point_size()
6266

6367
def _load_theme_color(self) -> None:
6468
"""加载主题颜色"""

app/core/font_manager.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,43 @@ def _apply_system_font(font_family: str, font_weight: int) -> str:
123123
return font_family
124124

125125

126+
def ensure_application_font_point_size(default_point_size: int = 9) -> None:
127+
"""确保应用程序字体具有有效的 pointSize,避免 pointSize 为 -1/0 的情况"""
128+
app_font = QApplication.font()
129+
try:
130+
point_size = int(app_font.pointSize())
131+
except Exception:
132+
point_size = -1
133+
134+
if point_size > 0:
135+
return
136+
137+
try:
138+
pixel_size = int(app_font.pixelSize())
139+
except Exception:
140+
pixel_size = 0
141+
142+
resolved_point_size = int(default_point_size) if int(default_point_size) > 0 else 9
143+
if pixel_size > 0:
144+
try:
145+
screen = QApplication.primaryScreen()
146+
dpi = float(screen.logicalDotsPerInch()) if screen is not None else 96.0
147+
if dpi > 0:
148+
resolved_point_size = max(1, int(round(pixel_size * 72.0 / dpi)))
149+
except Exception:
150+
pass
151+
152+
if resolved_point_size <= 0:
153+
resolved_point_size = 9
154+
155+
try:
156+
app_font.setPointSize(int(resolved_point_size))
157+
QApplication.setFont(app_font)
158+
logger.debug(f"已修正应用字体 pointSize: {resolved_point_size}")
159+
except Exception as e:
160+
logger.warning(f"修正应用字体 pointSize 失败: {e}")
161+
162+
126163
def configure_dpi_scale() -> None:
127164
"""在创建QApplication之前配置DPI缩放模式"""
128165
try:
@@ -170,6 +207,7 @@ def apply_font_settings() -> None:
170207
app_font.setFamily(actual_font_family)
171208
app_font.setWeight(get_font_weight_value(font_weight_int))
172209
QApplication.setFont(app_font)
210+
ensure_application_font_point_size()
173211
QTimer.singleShot(
174212
FONT_APPLY_DELAY,
175213
lambda: safe_execute(

main.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import sentry_sdk
99
from sentry_sdk.integrations.loguru import LoguruIntegration, LoggingLevels
10-
from PySide6.QtCore import Qt, QTimer
10+
from PySide6.QtCore import Qt, QTimer, qInstallMessageHandler
1111
from PySide6.QtWidgets import QApplication
1212
from loguru import logger
1313

@@ -31,7 +31,10 @@
3131
setup_local_server,
3232
send_url_to_existing_instance,
3333
)
34-
from app.core.font_manager import configure_dpi_scale
34+
from app.core.font_manager import (
35+
configure_dpi_scale,
36+
ensure_application_font_point_size,
37+
)
3538
from app.core.window_manager import WindowManager
3639
from app.core.url_handler_setup import create_url_handler
3740
from app.core.cs_ipc_handler_setup import create_cs_ipc_handler
@@ -262,6 +265,19 @@ def setup_qt_application():
262265
configure_dpi_scale()
263266

264267
app = QApplication(sys.argv)
268+
handler_holder = {"previous_handler": None}
269+
270+
def qt_message_handler(mode, context, message):
271+
if str(message).startswith("QFont::setPointSize: Point size <= 0"):
272+
return
273+
previous_handler = handler_holder.get("previous_handler")
274+
if previous_handler is not None:
275+
previous_handler(mode, context, message)
276+
else:
277+
sys.__stderr__.write(f"{message}\n")
278+
279+
handler_holder["previous_handler"] = qInstallMessageHandler(qt_message_handler)
280+
ensure_application_font_point_size()
265281

266282
gc.enable()
267283

0 commit comments

Comments
 (0)