PySide6 QMessageBox 详解


QMessageBox 是 PySide6 中用于显示模态对话框的组件,常用于提示信息、警告、错误或获取用户确认。以下是其核心功能、参数说明及完整示例。


1. 基础用法

快速显示静态方法

PySide6 提供了静态方法直接显示预设对话框:

from PySide6.QtWidgets import QMessageBox

# 信息提示框
QMessageBox.information(None, "标题", "这是一条信息提示。")

# 警告框
QMessageBox.warning(None, "警告", "操作可能导致数据丢失!")

# 错误框
QMessageBox.critical(None, "错误", "无法打开文件!")

# 提问框(返回用户点击的按钮类型)
result = QMessageBox.question(None, "确认", "确定要删除吗?")
if result == QMessageBox.StandardButton.Yes:
    print("用户确认删除")

2. 构造函数参数详解

通过实例化 QMessageBox 可自定义更多细节:

msg_box = QMessageBox(
    QMessageBox.Icon.Warning,        # 图标类型
    "警告标题",                       # 窗口标题
    "这是详细警告内容",                # 主文本
    QMessageBox.StandardButton.Ok |  # 按钮组合
    QMessageBox.StandardButton.Cancel
)
参数说明
参数类型说明
iconQMessageBox.Icon对话框图标,可选值:
NoIcon, Information, Warning, Critical, Question
titlestr窗口标题
textstr主显示文本
buttonsQMessageBox.StandardButton按钮组合(通过 `
parentQWidget父窗口(若为 None 则居中屏幕显示)

3. 自定义属性和方法

添加详细文本
msg_box.setDetailedText("错误详情:文件路径不存在。")
设置默认按钮
msg_box.setDefaultButton(QMessageBox.StandardButton.Cancel)
修改图标
msg_box.setIcon(QMessageBox.Icon.Critical)
自定义按钮文本
custom_button = msg_box.addButton("自定义按钮", QMessageBox.ButtonRole.ActionRole)

4. 按钮角色与响应处理

标准按钮类型
from PySide6.QtWidgets import QMessageBox

# 按钮组合示例
buttons = (
    QMessageBox.StandardButton.Yes |
    QMessageBox.StandardButton.No |
    QMessageBox.StandardButton.Help
)

msg_box.setStandardButtons(buttons)
捕获用户点击
result = msg_box.exec()  # 显示对话框并等待用户操作

if result == QMessageBox.StandardButton.Yes:
    print("用户点击了 Yes")
elif result == QMessageBox.StandardButton.Help:
    print("用户请求帮助")

5. 完整示例代码

from PySide6.QtWidgets import QApplication, QMessageBox, QPushButton, QWidget
from PySide6.QtCore import Qt

class DemoWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setup_ui()

    def setup_ui(self):
        self.setWindowTitle("QMessageBox 示例")
        self.resize(300, 200)

        button = QPushButton("显示对话框", self)
        button.clicked.connect(self.show_custom_dialog)
        button.move(100, 80)

    def show_custom_dialog(self):
        msg_box = QMessageBox(self)
        msg_box.setIcon(QMessageBox.Icon.Question)
        msg_box.setWindowTitle("确认操作")
        msg_box.setText("确定要提交数据吗?")
        msg_box.setInformativeText("提交后无法撤销!")
        msg_box.setDetailedText("数据详情:\n- 用户信息\n- 订单记录")

        # 添加自定义按钮
        save_button = msg_box.addButton("保存草稿", QMessageBox.ButtonRole.ActionRole)
        save_button.clicked.connect(self.save_draft)

        # 标准按钮
        msg_box.setStandardButtons(
            QMessageBox.StandardButton.Yes |
            QMessageBox.StandardButton.No |
            QMessageBox.StandardButton.Cancel
        )

        # 设置默认按钮
        msg_box.setDefaultButton(QMessageBox.StandardButton.No)

        # 显示对话框并处理结果
        result = msg_box.exec()
        if result == QMessageBox.StandardButton.Yes:
            print("数据已提交")
        elif result == QMessageBox.StandardButton.Cancel:
            print("操作已取消")

    def save_draft(self):
        print("草稿已保存")

if __name__ == "__main__":
    app = QApplication([])
    window = DemoWindow()
    window.show()
    app.exec()

6. 样式自定义(QSS)

通过样式表修改对话框外观:

msg_box.setStyleSheet("""
    QMessageBox {
        background-color: #f0f0f0;
        font-size: 14px;
    }
    QMessageBox QLabel#qt_msgbox_label {
        color: #333;
    }
    QMessageBox QPushButton {
        min-width: 80px;
        padding: 5px;
    }
""")

7. 核心枚举值

QMessageBox.Icon
说明
NoIcon无图标
Information信息图标(ⓘ)
Warning警告图标(⚠)
Critical错误图标(❌)
Question问题图标(?)
QMessageBox.StandardButton
说明
Ok确定
Cancel取消
Yes
No
Abort终止
Retry重试
Ignore忽略

总结

  • 静态方法:快速显示预设对话框(information()warning() 等)。
  • 构造函数:支持高度自定义(图标、按钮、文本)。
  • 信号处理:通过 exec() 返回值或按钮信号捕获用户操作。
  • 样式定制:使用 QSS 调整对话框外观。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值