修复问题

This commit is contained in:
2026-04-03 23:56:36 +08:00
parent 17655f2dba
commit 104dfe9693
6 changed files with 161 additions and 5 deletions
+44 -5
View File
@@ -1,12 +1,16 @@
"""标准对话框(QMessageBox / QInputDialog / QFileDialog)与当前主题一致。"""
from __future__ import annotations
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QTextOption
from PyQt6.QtWidgets import (
QFileDialog,
QInputDialog,
QMessageBox,
QWidget,
QDialog,
QLabel,
QSizePolicy,
)
import ui.theme as theme
@@ -29,7 +33,15 @@ def stylesheet() -> str:
QMessageBox QLabel {{
color: {fg};
background: transparent;
min-width: 160px;
min-width: 0px;
}}
QMessageBox QLabel#qt_msgbox_label {{
min-width: 200px;
max-width: 560px;
}}
QMessageBox QLabel#qt_msgboxex_icon_label {{
min-width: 28px;
max-width: 28px;
}}
QMessageBox QPushButton {{
min-width: 64px;
@@ -127,6 +139,34 @@ def _apply(w: QWidget | None) -> None:
w.setStyleSheet(stylesheet())
def _prepare_qmessagebox(msg: QMessageBox) -> None:
"""让提示框按内容换行并自适应宽高(避免固定宽度截断文字)。"""
lbl = msg.findChild(QLabel, "qt_msgbox_label")
if lbl is not None:
lbl.setWordWrap(True)
lbl.setTextFormat(Qt.TextFormat.PlainText)
# 无空格长中文也能在边界处断行,而不是整段挤在一行被裁切
try:
lbl.setWordWrapMode(QTextOption.WrapMode.WrapAnywhere)
except AttributeError:
pass
lbl.setMinimumWidth(200)
lbl.setMaximumWidth(560)
lbl.setSizePolicy(
QSizePolicy.Policy.Preferred,
QSizePolicy.Policy.MinimumExpanding,
)
lay = msg.layout()
if lay is not None:
lay.activate()
msg.setSizePolicy(
QSizePolicy.Policy.Preferred,
QSizePolicy.Policy.Preferred,
)
msg.setMinimumSize(0, 0)
msg.adjustSize()
def question(
parent: QWidget | None,
title: str,
@@ -146,8 +186,7 @@ def question(
if default_button is not None:
msg.setDefaultButton(default_button)
_apply(msg)
# 防止对话框因为文本长度/默认最小宽度变得过宽
msg.setFixedWidth(320)
_prepare_qmessagebox(msg)
return QMessageBox.StandardButton(msg.exec())
@@ -158,7 +197,7 @@ def warning(parent: QWidget | None, title: str, text: str) -> None:
msg.setIcon(QMessageBox.Icon.Warning)
msg.setStandardButtons(QMessageBox.StandardButton.Ok)
_apply(msg)
msg.setFixedWidth(320)
_prepare_qmessagebox(msg)
msg.exec()
@@ -169,7 +208,7 @@ def information(parent: QWidget | None, title: str, text: str) -> None:
msg.setIcon(QMessageBox.Icon.Information)
msg.setStandardButtons(QMessageBox.StandardButton.Ok)
_apply(msg)
msg.setFixedWidth(320)
_prepare_qmessagebox(msg)
msg.exec()