File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
03-QtWidgets-常用控件/12-Dialog-对话框/01-QDialog-对话框基类 Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ import sys
2+
3+ from PySide6 import QtWidgets
4+
5+ """
6+ QDialog 对话框类
7+ 对话框是一类用于与用户交互的控件,往往作为单独的窗口弹出
8+ 可以向用户展示信息(例如QMessageBox)、获取用户的选择(如通过QFileDialog获取用户选择的文件)等
9+ 官方文档:https://doc.qt.io/qtforpython/PySide6/QtWidgets/QDialog.html
10+ QDialog继承自QWidget,是所有对话框控件的基类
11+
12+ 只有一种构造函数,可选地将父控件传入
13+ .__init__(self, parent: Optional[PySide6.QtWidgets.QWidget]
14+
15+ 注意对话框控件设置父控件略有特殊:对话框始终为独立窗口(而不会成为父控件的一部分)、
16+ 如果为对话框设置了父控件,则其默认出现在父控件的顶级控件的上方(窗口在最上方,可以盖住下面
17+ 的内容;它还将共享父级的任务栏条目
18+ """
19+
20+
21+ class MyWidget (QtWidgets .QWidget ):
22+ def __init__ (self , * args , ** kwargs ):
23+ super ().__init__ (* args , ** kwargs )
24+ self .setWindowTitle ("QDialog" )
25+ self .resize (600 , 400 )
26+ self .setup_ui ()
27+
28+ def setup_ui (self ) -> None :
29+ """设置界面"""
30+
31+ # 设置对话框
32+ dialog = QtWidgets .QDialog (self )
33+ dialog .setWindowTitle ("这是一个对话框" )
34+ dialog .resize (300 , 200 )
35+ QtWidgets .QLabel ("模态对话框会影响主窗口关闭" , dialog )
36+
37+ # 在主窗口上弹出对话框
38+ test_btn = QtWidgets .QPushButton ("弹出对话框" , self )
39+ test_btn .move (200 , 200 )
40+ test_btn .clicked .connect (dialog .exec ) # type: ignore
41+
42+
43+ if __name__ == "__main__" :
44+ app = QtWidgets .QApplication (sys .argv )
45+ window = MyWidget ()
46+ window .show ()
47+ sys .exit (app .exec ())
You can’t perform that action at this time.
0 commit comments