当前位置:网站首页>QT ListView 列表显示组件笔记
QT ListView 列表显示组件笔记
2022-07-25 16:24:00 【洪水中】
新建一个应用
from PyQt5 import QtWidgets,QtCore,QtGui
import sys
app = QtWidgets.QApplication(sys.argv)
sys.exit(app.exec_())
新建一个窗口
# from PyQt5 import QtWidgets,QtCore,QtGui
# import sys
# app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
window.show()
# sys.exit(app.exec_())
新建列表展示控件
# from PyQt5 import QtWidgets,QtCore,QtGui
# import sys
# app = QtWidgets.QApplication(sys.argv)
# window = QtWidgets.QMainWindow()
# window.resize(350, 250)
listView = QtWidgets.QListView(window)
listView.setGeometry(0, 0, 350, 250)
# window.show()
# sys.exit(app.exec_())
给列表添加数据,python中的数据字符串列表无法直接传递给QListView,需要借助模型传递,这里使用QStringListModle
相关文章:QT5知识:字符串列表QStringListModel
添加stringListModel,并添加了数据, 获取model数据
#from PyQt5 import QtWidgets,QtCore,QtGui
#import sys
#app = QtWidgets.QApplication(sys.argv)
#window = QtWidgets.QMainWindow()
#window.resize(350, 250)
#listView = QtWidgets.QListView(window)
#listView.setGeometry(0, 0, 350, 250)
list = ["ItemA", "ItemB", "ItemC"]
model = QtCore.QStringListModel()
model.setStringList(list)
print(model.stringList())
listView.setModel(model)
#window.show()
s#ys.exit(app.exec_())
添加数据还可以使用另外一种方式,在学习另一种添加方式前,我们先定义一个点击事件
#from PyQt5 import QtWidgets,QtCore,QtGui
#import sys
def lv_clicked(index):
print(index.data())
#app = QtWidgets.QApplication(sys.argv)
#window = QtWidgets.QMainWindow()
#window.resize(350, 250)
#listView = QtWidgets.QListView(window)
#listView.setGeometry(0, 0, 350, 250)
#list = ["ItemA", "ItemB", "ItemC"]
#model = QtCore.QStringListModel()
#model.setStringList(list)
#print(model.stringList())
#listView.setModel(model)
listView.clicked.connect(lv_clicked)
#window.show()
#sys.exit(app.exec_())
每次点击列表时会打印选中的列表成员
接下来可以尝试删除列表成员
def lv_clicked(index):
print(index.data())
model.removeRow(0)
每次点击列表,最上面的成员就被删除
如果要删除选中的成员,应该可以猜出来吧
def lv_clicked(index):
print(index.row())
model.removeRow(index.row())
所以另外一种添加数据的方式就是使用insertRow(),查询这个命令构造,会发现和removeRow相同,只需要接受一个序号用来指定删除和添加的位置。那么添加的值如何指定?
这个命令会先插入一个空值,然后再给这个成员设置值
def lv_clicked(index):
row = index.row()
model.insertRow(row)
index = model.index(row)
print(index)
model.setData(index, "aaa")
边栏推荐
猜你喜欢

使用 Terraform 在 AWS 上快速部署 MQTT 集群

leetcode:6127. 优质数对的数目【位运算找规律 + 两数之和大于等于k + 二分】

2W word detailed data Lake: concept, characteristics, architecture and cases

Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%

使用Huggingface在矩池云快速加载预训练模型和数据集

【图像去噪】基于双立方插值和稀疏表示实现图像去噪matlab源码

城市燃气安全再拉警钟,如何防患于未“燃”?

百度富文本编辑器UEditor单张图片上传跨域

Paper notes: highly accurate protein structure prediction with alphafold (alphafold 2 & appendix)

Win11桌面切换快捷键是什么?Win11快速切换桌面的方法
随机推荐
mysql 隔离级别事务
使用Huggingface在矩池云快速加载预训练模型和数据集
Which led display manufacturer is better
使用 Terraform 在 AWS 上快速部署 MQTT 集群
MyBaits
Today, I went to oppo for an interview and was asked numbly
Waterfall flow layout
Emqx cloud update: more parameters are added to log analysis, which makes monitoring, operation and maintenance easier
测试驱动开发(TDD)在线练功房 | 9月17日开课
0x80131500 solution for not opening Microsoft Store
Win11桌面切换快捷键是什么?Win11快速切换桌面的方法
doGet与doPost
哪个led显示屏厂家更好
SAP Fiori 的附件处理(Attachment handling)
The second revolution of reporting tools
用递归进行数组求和
Gap Locks(间隙锁)
C# 音乐
Understanding service governance in distributed development
今天睡眠质量记录84分