当前位置:网站首页>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")
边栏推荐
- Google Earth Engine——全球建筑物GlobalMLBuildingFootprints矢量集合下载
- Gap Locks(间隙锁)
- Promise date
- How to build an enterprise level OLAP data engine for massive data and high real-time requirements?
- How does win11's own drawing software display the ruler?
- 如何安装govendor并打开项目
- MyBaits
- 城市燃气安全再拉警钟,如何防患于未“燃”?
- 使用Huggingface在矩池云快速加载预训练模型和数据集
- MySQL implicit lock
猜你喜欢

Win11自带画图软件怎么显示标尺?

MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具

Communication between processes (pipeline details)

伦敦银K线图的各种有用形态

Product upgrade observation station in June

可验证随机函数 VRF

【ZeloEngine】反射系统填坑小结

Win11动态磁贴没了?Win11中恢复动态磁贴的方法

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

Cookie、cookie与session区别
随机推荐
泰雷兹推出解决方案,助力SAP客户控制云端数据
解决Win10磁盘占用100%
Typescript learning 2 - Interface
聊聊如何用 Redis 实现分布式锁?
MySQL全局锁
共享锁(Shared Lock)
01.一个更简单的方法来传递大量的props
Test Driven Development (TDD) online practice room | classes open on September 17
Today, I went to oppo for an interview and was asked numbly
Fastadmin TP installation uses Baidu rich text editor ueeditor
MySQL isolation level transactions
tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框
百度富文本编辑器UEditor 图片宽度100%自适应,手机端
MySQL check whether the table is locked
邮件的收发的展现逻辑之收件箱发件箱以及回复断链的问题
[wechat applet] detailed explanation of applet host environment
01. A simpler way to deliver a large number of props
Which led display manufacturer is better
leetcode:528. 按权重随机选择【普通随机失效 + 要用前缀和二分】
MySQL显式锁