Skip to content

Commit 87fa493

Browse files
committed
docs(pypi-package): update pyside6
1 parent 2a04b22 commit 87fa493

File tree

12 files changed

+360
-14
lines changed

12 files changed

+360
-14
lines changed

docs/pypi-package/pyside6/chapter04/index.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
[[TOC]]
44

5+
## 窗口置顶
6+
7+
@[code python](./src/stay_top.py)
8+
59
## QButtonGroup
610

711
在 Qt Designer 中,选择多个 `RadioButton` 对象右键,选择分配给按钮组,新建按钮组即可创建一个 `QButtonGroup`
@@ -60,3 +64,13 @@
6064
| `>AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#` | 许可证号码,大写,空白为 `#` |
6165

6266
如果我需要设计一个输入 4 字节十六进制数字的输入框,可以使用 `>HH-HH-HH-HH;_` 来表示。
67+
68+
## 创建文件浏览器
69+
70+
可以参考 [`QFileSystemModel`](https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QFileSystemModel.html) 来了解详细信息。
71+
72+
@[code python](./src/file_explorer.py)
73+
74+
## 简单浏览器
75+
76+
@[code python](./src/simple_brower.py)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import os
2+
import sys
3+
4+
from PySide6.QtCore import QDir, QModelIndex
5+
from PySide6.QtGui import QStandardItem, QStandardItemModel
6+
from PySide6.QtWidgets import (
7+
QApplication,
8+
QFileSystemModel,
9+
QHBoxLayout,
10+
QTreeView,
11+
QWidget,
12+
)
13+
14+
15+
class MainWidget(QWidget):
16+
def __init__(self, parent=None):
17+
super().__init__(parent)
18+
self.setWindowTitle("文件浏览器")
19+
20+
# 文件系统模型,只显示文件夹
21+
self._file_model = QFileSystemModel()
22+
self._file_model.setFilter(QDir.Dirs | QDir.NoDotAndDotDot) # type: ignore
23+
self._file_model.setRootPath("")
24+
25+
# 创建目录树视图
26+
self._tree_view = QTreeView(self)
27+
self._tree_view.setModel(self._file_model)
28+
self._tree_view.setHeaderHidden(True)
29+
30+
# 隐藏:文件大小、类型、修改时间
31+
for col in range(1, 4):
32+
self._tree_view.setColumnHidden(col, True)
33+
34+
# 设置双击为展开文件到列表
35+
self._tree_view.doubleClicked.connect(self.flush_filelist)
36+
37+
# 文件列表模型
38+
self._filelist_model = QStandardItemModel()
39+
40+
# 创建文件列表视图
41+
self._filelist_view = QTreeView(self)
42+
self._filelist_view.setModel(self._filelist_model)
43+
self._filelist_view.setEditTriggers(QTreeView.NoEditTriggers)
44+
45+
# 添加布局
46+
layout = QHBoxLayout()
47+
layout.addWidget(self._tree_view)
48+
layout.addWidget(self._filelist_view)
49+
self.setLayout(layout)
50+
51+
def flush_filelist(self, index: QModelIndex):
52+
"""刷新文件列表"""
53+
self._filelist_model.clear()
54+
path = self._file_model.filePath(index)
55+
# 设置列表头
56+
self._filelist_model.setHorizontalHeaderLabels([path])
57+
58+
# 遍历文件夹下的文件
59+
for file in os.listdir(path):
60+
if os.path.isfile(os.path.join(path, file)):
61+
item = QStandardItem(file)
62+
self._filelist_model.appendRow(item)
63+
64+
65+
if __name__ == "__main__":
66+
app = QApplication(sys.argv)
67+
window = MainWidget()
68+
window.resize(600, 400)
69+
window.show()
70+
sys.exit(app.exec())
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import sys
2+
3+
from PySide6.QtWebEngineWidgets import QWebEngineView
4+
from PySide6.QtWidgets import QApplication, QMainWindow
5+
6+
7+
class MainWindow(QMainWindow):
8+
def __init__(self):
9+
super().__init__()
10+
self.setWindowTitle("Web View")
11+
self._browser = QWebEngineView()
12+
self._browser.load("https://www.bing.com/")
13+
self.setCentralWidget(self._browser)
14+
15+
16+
if __name__ == "__main__":
17+
app = QApplication(sys.argv)
18+
window = MainWindow()
19+
window.showMaximized()
20+
app.exit(app.exec())
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import sys
2+
3+
from PySide6.QtCore import Qt
4+
from PySide6.QtWidgets import QApplication, QWidget
5+
6+
if __name__ == "__main__":
7+
app = QApplication(sys.argv)
8+
window = QWidget()
9+
window.setWindowFlags(Qt.WindowStaysOnTopHint)
10+
window.show()
11+
sys.exit(app.exec())
Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1-
# 7. PySide6 样式和 QSS
1+
# 7. PySide6 样式和动画
22

33
[[TOC]]
4+
5+
## QSS 基本语法
6+
7+
::: tip 参考文档
8+
9+
- [QSS 可用属性:官方文档](https://doc.qt.io/qt-6/stylesheet-reference.html#list-of-properties)
10+
- [QSS 官方示例](https://doc.qt.io/qt-6/stylesheet-examples.html)
11+
12+
:::
13+
14+
类似 CSS,QSS 每一条都是由一个选择器和一组声明构成:
15+
16+
- 选择器选出要对哪种控件进行样式修改
17+
- 每个声明都是键值对,键为属性,值为属性值
18+
19+
```css
20+
QWidget {
21+
color: #000;
22+
background-color: #fff;
23+
}
24+
```
25+
26+
## 使用方式
27+
28+
为降低耦合,往往把 QSS 写在一个单独的 `style.qss` 文件中,然后在 `main.py``QMainWindow` 中加载样式。
29+
30+
新建一个扩展名为 `.qss` 的文件,如 `style.qss`,编辑内容。
31+
32+
@[code css](./src/style.qss)
33+
34+
`main.py` 中加载样式:
35+
36+
@[code python](./src/qss_demo.py)
37+
38+
## 动态修改
39+
40+
部分参考了 [hektorprofe/curso-qt-pyside-udemy](https://github.com/hektorprofe/curso-qt-pyside-udemy) 的代码。
41+
42+
@[code python](./src/qss_editor.py)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sys
2+
3+
from PySide6.QtCore import QFile, Qt
4+
from PySide6.QtWidgets import QApplication, QLabel, QMainWindow
5+
6+
7+
class QSSLoader:
8+
def __init__(self, path: str) -> None:
9+
self._path = path
10+
11+
def load(self) -> str:
12+
f = QFile(self._path)
13+
f.open(QFile.ReadOnly | QFile.Text)
14+
stylesheet = f.readAll()
15+
return stylesheet.data().decode("utf-8")
16+
17+
18+
class MainWindow(QMainWindow):
19+
def __init__(self):
20+
super().__init__()
21+
self.setWindowTitle("QSS Demo")
22+
self.resize(400, 300)
23+
24+
label = QLabel("Hello World")
25+
label.setAlignment(Qt.AlignCenter)
26+
self.setCentralWidget(label)
27+
28+
29+
if __name__ == "__main__":
30+
app = QApplication(sys.argv)
31+
window = MainWindow()
32+
window.setStyleSheet(QSSLoader("style.qss").load())
33+
window.show()
34+
sys.exit(app.exec())
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import sys
2+
3+
from PySide6.QtWidgets import (
4+
QApplication,
5+
QCheckBox,
6+
QFormLayout,
7+
QLabel,
8+
QLineEdit,
9+
QMainWindow,
10+
QPlainTextEdit,
11+
QPushButton,
12+
QRadioButton,
13+
QSpinBox,
14+
QVBoxLayout,
15+
QWidget,
16+
)
17+
18+
STYLE = """QMainWindow {
19+
background-color: #212121;
20+
}
21+
QLabel {
22+
color: #e9e9e9;
23+
}
24+
QPushButton {
25+
background-color: orange;
26+
font-family: 'Arial';
27+
font-size: 14px;
28+
font-weight: bold;
29+
}
30+
"""
31+
32+
33+
class QSSEditor(QWidget):
34+
def __init__(self, parent: QWidget | None = None):
35+
super().__init__()
36+
37+
self._parent = parent
38+
self.resize(480, 320)
39+
self.setWindowTitle("QSS Editor")
40+
41+
self._editor = QPlainTextEdit()
42+
self._editor.setStyleSheet(
43+
"background-color: #212121;color: #e9e9e9;"
44+
"font-family: Consolas; font-size: 16px; "
45+
)
46+
self._editor.setFont("Consolas")
47+
self._editor.setPlainText(STYLE)
48+
self._editor.textChanged.connect(self.update_style)
49+
50+
layout = QVBoxLayout()
51+
layout.addWidget(self._editor)
52+
self.setLayout(layout)
53+
54+
self.show()
55+
56+
def update_style(self):
57+
qss = self._editor.toPlainText()
58+
try:
59+
self._parent.setStyleSheet(qss)
60+
except:
61+
pass
62+
63+
64+
class MainWindow(QMainWindow):
65+
def __init__(self):
66+
super().__init__()
67+
68+
layout = QFormLayout()
69+
layout.addRow("QCheckBox", QCheckBox())
70+
layout.addRow("QRadioButton", QRadioButton())
71+
layout.addRow("QLabel", QLabel("QLabel"))
72+
layout.addRow("QPushButton", QPushButton("QPushButton"))
73+
layout.addRow("QLineEdit", QLineEdit("QLineEdit"))
74+
layout.addRow("QSpinBox", QSpinBox())
75+
76+
widget = QWidget()
77+
widget.setLayout(layout)
78+
self.setCentralWidget(widget)
79+
80+
label = QLabel("QLabel")
81+
label.setObjectName("label")
82+
layout.addRow(label)
83+
84+
self._qss_editor = QSSEditor(self)
85+
self.setStyleSheet(STYLE)
86+
87+
88+
if __name__ == "__main__":
89+
app = QApplication(sys.argv)
90+
window = MainWindow()
91+
window.show()
92+
sys.exit(app.exec())
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
QWidget {
2+
color: #e34fff;
3+
background-color: #000;
4+
}

docs/pypi-package/pyside6/chapter08/src/hello.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22
import sys
33

44
from PySide6.QtCore import Qt, Slot
5-
from PySide6.QtWidgets import (QApplication, QLabel, QPushButton, QVBoxLayout,
6-
QWidget)
5+
from PySide6.QtWidgets import QApplication, QLabel, QPushButton, QVBoxLayout, QWidget
76

87

98
class MyWidget(QWidget):
109
def __init__(self):
1110
QWidget.__init__(self)
1211

13-
self.hello = ['Hallo Welt', '你好,世界', 'Hei maailma',
14-
'Hola Mundo', 'Привет мир']
12+
self.hello = ["Hallo Welt", "你好,世界", "Hei maailma", "Hola Mundo", "Привет мир"]
1513

16-
self.button = QPushButton('Click me!')
17-
self.text = QLabel('Hello World')
14+
self.button = QPushButton("Click me!")
15+
self.text = QLabel("Hello World")
1816
self.text.setAlignment(Qt.AlignCenter)
1917

2018
self.layout = QVBoxLayout()
@@ -30,11 +28,9 @@ def magic(self):
3028
self.text.setText(random.choice(self.hello))
3129

3230

33-
if __name__ == '__main__':
31+
if __name__ == "__main__":
3432
app = QApplication(sys.argv)
35-
3633
widget = MyWidget()
3734
widget.resize(800, 600)
3835
widget.show()
39-
4036
sys.exit(app.exec())
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 9. 使用 PySide6 构建高级应用
2+
3+
[[TOC]]
4+
5+
## 9.1 使用 OpenCV
6+
7+
如果没有安装 OpenCV,可以使用下面的命令安装:
8+
9+
```bash
10+
pip install opencv-python
11+
```
12+
13+
下面我们来看一个使用 OpenCV 的例子,使用 OpenCV 读取摄像头的图像,并显示在窗口中。
14+
15+
@[code python](./src/opencv_read_camera.py)

0 commit comments

Comments
 (0)