Skip to content

Commit 5e47d54

Browse files
committed
Add 03-12-05-04
QMessageBox-静态方法
1 parent c8690f3 commit 5e47d54

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
"""
2+
QMessageBox 静态方法
3+
4+
在某些场景下,只需显示一些简单的信息,并不需要实例化一个QMessageBox对象并对其各种属性进行复杂精细的设置,
5+
而只需调用QMessageBox提供的静态方法即可满足这些需求
6+
7+
此处简单列出这些静态方法与其作用,参数返回值详解见下方讲解:
8+
- about() 显示关于信息
9+
- aboutQt() 显示当前程序使用的Qt的关于信息
10+
- information() 显示信息
11+
- question() 显示问题
12+
- warning() 显示警告
13+
- critical() 显示严重错误
14+
15+
16+
.about(parent: QWidget, title: str, text: str) -> None
17+
about方法用于显示一个简单的「关于」对话框,title、text、parent对应其窗口标题、正文文本、父控件,该窗口还有一个OK按钮
18+
about会尝试寻找合适的图标应用于自身,具体规则参考https://doc.qt.io/qt-6/qmessagebox.html#about
19+
20+
.aboutQt(parent: QWidget, title:str = QString()) -> None
21+
显示一个关于Qt的简易信息框,使用给定的窗口标题,并在位置上以父控件为中心。此消息中包含应用程序正在使用的Qt版本号
22+
常用于应用程序的帮助菜单;QApplication 以槽的形式提供此功能
23+
24+
.information(parent: QWidget, title: str, text: str, button0: QMessageBox.StandardButton, \
25+
button1: QMessageBox.StandardButton = QMessageBox.StandardButton.NoButton) -> QMessageBox.StandardButton
26+
.information(parent: QWidget, title: str, text: str, buttons: QMessageBox.StandardButtons = \
27+
QMessageBox.StandardButton.Ok, defaultButton: QMessageBox.StandardButton = QMessageBox.StandardButton.NoButton) -> \
28+
QMessageBox.StandardButton
29+
30+
在指定的父窗口前打开一个具有指定标题和文本的信息提示框;
31+
添加指定的标准按钮有两种方式(重载),若传入一组按钮(通过"|"连接),则必须将其中之一指定为defaultButton;
32+
此静态方法会返回用户点击的标准按钮的id,如果用户通过Esc关闭对话框,则返回escape按钮;
33+
此信息框为应用程序级模态对话框。
34+
35+
question()、warning()、critical() 静态方法与 information() 非常相似,类比使用即可,不再赘述。
36+
37+
38+
"""
39+
40+
import sys
41+
42+
from PySide6 import QtCore
43+
from PySide6.QtWidgets import QApplication, QHBoxLayout, QLabel, QMessageBox, QPushButton, QWidget
44+
45+
46+
class MyWidget(QWidget):
47+
def __init__(self, *args, **kwargs):
48+
super().__init__(*args, **kwargs)
49+
self.about_btn = QPushButton("关于此程序")
50+
self.about_qt_btn = QPushButton("关于Qt")
51+
self.information_btn = QPushButton("展示信息")
52+
self.warning_btn = QPushButton("展示警告")
53+
self.label = QLabel()
54+
self.setup_ui()
55+
self.test_static()
56+
57+
def setup_ui(self) -> None:
58+
"""设置界面"""
59+
self.setWindowTitle("QMessageBox-静态方法")
60+
self.setMinimumSize(600, 180)
61+
62+
layout = QHBoxLayout()
63+
layout.addWidget(self.about_btn)
64+
layout.addWidget(self.about_qt_btn)
65+
layout.addWidget(self.information_btn)
66+
layout.addWidget(self.warning_btn)
67+
layout.addWidget(self.label)
68+
self.setLayout(layout)
69+
70+
def test_static(self):
71+
"""测试QMessageBox的静态方法"""
72+
73+
self.about_btn.clicked.connect(
74+
lambda: QMessageBox.about(self, "关于此程序", "本程序为PySide6 Code Tutorial中的一个案例")
75+
) # type: ignore
76+
self.about_qt_btn.clicked.connect(lambda: QMessageBox.aboutQt(self)) # type: ignore
77+
78+
user_result_dict = {
79+
QMessageBox.Ok: "OK",
80+
QMessageBox.Cancel: "取消",
81+
QMessageBox.Discard: "不保存",
82+
QMessageBox.Save: "保存",
83+
QMessageBox.Apply: "应用",
84+
}
85+
86+
@QtCore.Slot()
87+
def show_info_dlg() -> None:
88+
"""self.information_btn对应的槽函数"""
89+
result = QMessageBox.information(
90+
self,
91+
"静态方法-信息",
92+
"这里可以展示一些信息",
93+
QMessageBox.Ok,
94+
QMessageBox.Cancel,
95+
) # 调用静态函数展示信息,将返回值保存到result变量中
96+
self.label.setText(f"用户选择了:{user_result_dict[result]}")
97+
self.label.adjustSize()
98+
99+
@QtCore.Slot()
100+
def show_warn_dlg() -> None:
101+
"""self.warning_btn对应的槽函数"""
102+
result = QMessageBox.warning(self, "静态方法-警告", "警告:直接退出将不会保存修改", QMessageBox.Discard)
103+
self.label.setText(f"用户选择了:{user_result_dict[result]}")
104+
self.label.adjustSize()
105+
106+
self.information_btn.clicked.connect(show_info_dlg) # type: ignore
107+
self.warning_btn.clicked.connect(show_warn_dlg) # type: ignore
108+
109+
110+
if __name__ == "__main__":
111+
app = QApplication(sys.argv)
112+
window = MyWidget()
113+
window.show()
114+
sys.exit(app.exec())

0 commit comments

Comments
 (0)