You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyQt6中QMessageBox子部件显示被裁剪的问题求助

PyQt6 QMessageBox 控件被裁剪的解决办法

在Linux(Ubuntu 22.04)环境下,切换到PyQt6 6.9.0后,QMessageBox.question()及自定义QMessageBox出现子部件被裁剪的问题,文本、按钮、图标均被截断。复现代码如下:

import sys
from PyQt6 import QtWidgets

app = QtWidgets.QApplication(sys.argv)

response = QtWidgets.QMessageBox.question(None, 'Question', 'Do you want to continue?',
QtWidgets.QMessageBox.StandardButton.Yes | QtWidgets.QMessageBox.StandardButton.No)

print("Response:", response)

sys.exit()

环境版本:

  • Python: 3.10.12
  • PyQt6: 6.9.0
  • PyQt6-Qt6: 6.9.0
  • PyQt6_sip: 13.10.0

解决办法

1. 手动调整对话框布局与大小

通过显式创建QMessageBox实例,强制触发布局更新和尺寸调整,避免控件截断:

import sys
from PyQt6 import QtWidgets

app = QtWidgets.QApplication(sys.argv)

msg_box = QtWidgets.QMessageBox(None)
msg_box.setWindowTitle('Question')
msg_box.setText('Do you want to continue?')
msg_box.setStandardButtons(QtWidgets.QMessageBox.StandardButton.Yes | QtWidgets.QMessageBox.StandardButton.No)

# 强制更新布局并适配尺寸
msg_box.layout().activate()
msg_box.adjustSize()

response = msg_box.exec()
print("Response:", response)

sys.exit()

2. 调整高DPI缩放设置

Linux环境下的高DPI适配可能导致Qt布局异常,可在初始化应用前修改相关属性:

import sys
from PyQt6 import QtWidgets
from PyQt6.QtCore import Qt

# 禁用自动高DPI缩放(或根据需求选择启用系统原生缩放)
QtWidgets.QApplication.setAttribute(Qt.ApplicationAttribute.AA_DisableHighDpiScaling)
# 可选:启用高DPI位图支持
# QtWidgets.QApplication.setAttribute(Qt.ApplicationAttribute.AA_UseHighDpiPixmaps)

app = QtWidgets.QApplication(sys.argv)

response = QtWidgets.QMessageBox.question(None, 'Question', 'Do you want to continue?',
QtWidgets.QMessageBox.StandardButton.Yes | QtWidgets.QMessageBox.StandardButton.No)

print("Response:", response)

sys.exit()

3. 降级PyQt6版本

PyQt6 6.9.0可能存在Linux平台的布局bug,可尝试降级至稳定版本:

pip install PyQt6==6.8.0 PyQt6-Qt6==6.8.0

4. 自定义对话框替代

若上述方法无效,可自行实现简单对话框确保布局正常:

import sys
from PyQt6 import QtWidgets

class CustomDialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle('Question')
        main_layout = QtWidgets.QVBoxLayout(self)
        
        # 添加提示文本
        text_label = QtWidgets.QLabel('Do you want to continue?')
        main_layout.addWidget(text_label)
        
        # 添加按钮组
        btn_layout = QtWidgets.QHBoxLayout()
        yes_btn = QtWidgets.QPushButton('Yes')
        no_btn = QtWidgets.QPushButton('No')
        btn_layout.addWidget(yes_btn)
        btn_layout.addWidget(no_btn)
        main_layout.addLayout(btn_layout)
        
        # 绑定按钮逻辑
        yes_btn.clicked.connect(self.accept)
        no_btn.clicked.connect(self.reject)

app = QtWidgets.QApplication(sys.argv)
dialog = CustomDialog()
result = dialog.exec()
# 映射原QMessageBox返回值格式
response = QtWidgets.QMessageBox.StandardButton.Yes if result == QtWidgets.QDialog.DialogCode.Accepted else QtWidgets.QMessageBox.StandardButton.No
print("Response:", response)

sys.exit()

内容的提问来源于stack exchange,提问作者nottelling

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 10:03:13