-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy path01-QListView-创建、方向、布局模式.py
45 lines (34 loc) · 1.44 KB
/
01-QListView-创建、方向、布局模式.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import sys
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QListView")
self.resize(500, 500)
self.move(400, 250)
self.data_list = [f"Item{i}" for i in range(40)] # 将数据列表保存在属性中
self.setup_ui()
def setup_ui(self):
list_view = QListView(self) # 创建list view对象
slm = QStringListModel() # 创建model模型
slm.setStringList(self.data_list)
list_view.setModel(slm) # 为视图设置模型
list_view.clicked.connect(self.clicked_list) # 会传出用户点击项的索引
# -------方向---------
# list_view.setFlow(QListView.LeftToRight)
list_view.setFlow(QListView.TopToBottom)
# ------布局模式(分批列出)-------
list_view.setBatchSize(5) # 每批列出的项目个数
# list_view.setLayoutMode(QListView.Batched) # 分批列出
list_view.setLayoutMode(QListView.SinglePass) # 一次性列出
def clicked_list(self, model_index):
# 弹出一个消息提升框,展示用户点击哪个项
QMessageBox.information(
self, "QListView", "你选择了: " + self.data_list[model_index.row()]
)
print("点击的是:" + str(model_index.row()))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())