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
)
参数说明
参数 | 类型 | 说明 |
---|---|---|
icon | QMessageBox.Icon | 对话框图标,可选值:NoIcon , Information , Warning , Critical , Question |
title | str | 窗口标题 |
text | str | 主显示文本 |
buttons | QMessageBox.StandardButton | 按钮组合(通过 ` |
parent | QWidget | 父窗口(若为 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 调整对话框外观。