Skip to content

Commit 86a7398

Browse files
committed
Add 03-12-03-01
QFileDialog-简介与创建
1 parent 9aeb9be commit 86a7398

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import sys
2+
3+
from PySide6 import QtWidgets
4+
5+
"""
6+
QFileDialog 文件对话框
7+
8+
用户可以通过文件对话框浏览文件或目录,常用于选择打开文件、将当前文件保存到某处等场景
9+
官方文档:https://doc.qt.io/qtforpython/PySide6/QtWidgets/QFileDialog.html
10+
继承自 QDialog
11+
12+
有两种构造函数,简介版只可选地传入父控件与窗口标志,完整版可以在创建时定义更多属性功能:
13+
.__init__(self, parent: QWidget, f: QtCore.Qt.WindowFlags)
14+
.__init__(self, parent: Optional[QWidget] = None, caption: str = '', directory: str = '', filter: str = '')
15+
16+
"""
17+
18+
19+
class MyWidget(QtWidgets.QWidget):
20+
def __init__(self, *args, **kwargs):
21+
super().__init__(*args, **kwargs)
22+
self.setWindowTitle("QFileDialog-简介")
23+
self.dialog = QtWidgets.QFileDialog(self)
24+
self.setup_dialog()
25+
self.setup_ui()
26+
27+
def setup_dialog(self) -> None:
28+
"""配置对话框属性功能"""
29+
self.dialog.setAcceptMode(QtWidgets.QFileDialog.AcceptOpen) # 打开模式
30+
self.dialog.setFileMode(QtWidgets.QFileDialog.ExistingFiles) # 选择现有文件
31+
self.dialog.setLabelText(QtWidgets.QFileDialog.Accept, "选择") # 为「接受」按键指定文本
32+
self.dialog.setLabelText(QtWidgets.QFileDialog.Reject, "取消") # 为「拒绝」按键指定文本
33+
34+
def setup_ui(self) -> None:
35+
"""设置界面"""
36+
self.resize(800, 600)
37+
browse_btn = QtWidgets.QPushButton("选择文件", self)
38+
browse_btn.move(150, 200)
39+
browse_btn.clicked.connect(self.dialog.open) # type: ignore
40+
info_le = QtWidgets.QLineEdit(self)
41+
info_le.move(150, 250)
42+
info_le.setMinimumWidth(600)
43+
self.dialog.fileSelected.connect(lambda path: info_le.setText(path)) # type: ignore
44+
45+
46+
if __name__ == "__main__":
47+
app = QtWidgets.QApplication(sys.argv)
48+
window = MyWidget()
49+
window.show()
50+
sys.exit(app.exec())

0 commit comments

Comments
 (0)