Skip to content

Commit 4bdb73f

Browse files
committed
✨ feat(QColumnView): 文件系统浏览器
1 parent b8b021f commit 4bdb73f

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

QColumnView/FileManager.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Created on 2025/04/08
6+
@author: Irony
7+
@site: https://pyqt.site | https://github.com/PyQt5
8+
@email: 892768447@qq.com
9+
@file: FileManager.py
10+
@description:
11+
"""
12+
13+
try:
14+
from PyQt5.QtWidgets import (
15+
QApplication,
16+
QColumnView,
17+
QFileSystemModel,
18+
QGridLayout,
19+
QMessageBox,
20+
QPushButton,
21+
QSizePolicy,
22+
QSpacerItem,
23+
QWidget,
24+
)
25+
except Exception:
26+
from PySide2.QtWidgets import (
27+
QApplication,
28+
QColumnView,
29+
QFileSystemModel,
30+
QGridLayout,
31+
QMessageBox,
32+
QPushButton,
33+
QSizePolicy,
34+
QSpacerItem,
35+
QWidget,
36+
)
37+
38+
39+
class FileManager(QWidget): # type: ignore
40+
def __init__(self, *args, **kwargs):
41+
super(FileManager, self).__init__(*args, **kwargs)
42+
self.resize(800, 600)
43+
self._view = QColumnView(self)
44+
self._btn = QPushButton("确定", self)
45+
layout = QGridLayout(self)
46+
layout.addWidget(self._view, 0, 0, 1, 2)
47+
layout.addItem(
48+
QSpacerItem(
49+
40,
50+
20,
51+
QSizePolicy.Expanding,
52+
QSizePolicy.Minimum,
53+
),
54+
1,
55+
0,
56+
)
57+
layout.addWidget(self._btn, 1, 1, 1, 1)
58+
layout.setRowStretch(1, 0)
59+
self._model = QFileSystemModel(self)
60+
self._model.setRootPath("") # 设置根路径
61+
self._view.setModel(self._model) # type: ignore
62+
self._btn.clicked.connect(self.onAccept) # type: ignore
63+
64+
def onAccept(self):
65+
path = self._model.filePath(self._view.currentIndex())
66+
print("path", path)
67+
if path:
68+
QMessageBox.information(self, "提示", "路径:%s" % path)
69+
70+
71+
if __name__ == "__main__":
72+
import cgitb
73+
import sys
74+
75+
cgitb.enable(format="text")
76+
app = QApplication(sys.argv)
77+
w = FileManager()
78+
w.show()
79+
sys.exit(app.exec_())

QColumnView/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# QColumnView
2+
3+
- 目录
4+
- [文件系统浏览器](#1文件系统浏览器)
5+
6+
## 1、文件系统浏览器
7+
8+
[运行 FileManager.py](FileManager.py)
9+
10+
一个省市区关联的三级联动,数据源在data.json中
11+
12+
1. 通过`QFileSystemModel`模型来显示文件系统
13+
2. 结合`QColumnView``setMode`函数显示文件系统模型
14+
3. 通过`QFileSystemModel.filePath(QColumnView.currentIndex())`获取当前选中的路径
15+
16+
![FileManager](ScreenShot/FileManager.png)
69.7 KB
Loading

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
- [QTableView](QTableView)
6969
- [表格内容复制](QTableView/CopyContent.py)
7070
- [QColumnView](QColumnView)
71+
- [文件系统浏览器](QColumnView/FileManager.py)
7172
- [QUndoView](QUndoView)
7273

7374
- Item Widgets

0 commit comments

Comments
 (0)