当前位置:网站首页>QT listview list display component notes
QT listview list display component notes
2022-07-25 16:31:00 【In flood】
Create an app
from PyQt5 import QtWidgets,QtCore,QtGui
import sys
app = QtWidgets.QApplication(sys.argv)
sys.exit(app.exec_())
Create a new window
# from PyQt5 import QtWidgets,QtCore,QtGui
# import sys
# app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
window.show()
# sys.exit(app.exec_())
New list display control
# 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_())
Add data to the list ,python The list of data strings in cannot be passed directly to QListView, You need to transfer with the help of models , Use here QStringListModle
Related articles :QT5 knowledge : String list QStringListModel
add to stringListModel, And added data , obtain model data
#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_())
There is another way to add data , Before learning another way to add , Let's first define a click event
#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_())
Each time you click on the list, the selected list members will be printed
Next, you can try to delete the list members
def lv_clicked(index):
print(index.data())
model.removeRow(0)
List per click , The top member is deleted
If you want to delete the selected member , It should be possible to guess
def lv_clicked(index):
print(index.row())
model.removeRow(index.row())
So another way to add data is to use insertRow(), Query this command construct , Will find and removeRow identical , You only need to accept a sequence number to specify the location of deletion and addition . Then how to specify the added value ?
This command will first insert a null value , Then set the value for this member
def lv_clicked(index):
row = index.row()
model.insertRow(row)
index = model.index(row)
print(index)
model.setData(index, "aaa")
边栏推荐
猜你喜欢

狂神redis笔记12

论文笔记:Highly accurate protein structure prediction with AlphaFold (AlphaFold 2 & appendix)

终极套娃 2.0 | 云原生交付的封装
![Leetcode:154. find the minimum value II in the rotation sort array [about the middle and rear positioning dichotomy of the rotation sort array]](/img/03/54a2d82a17cd07374dc0aedacd7b11.png)
Leetcode:154. find the minimum value II in the rotation sort array [about the middle and rear positioning dichotomy of the rotation sort array]

IaaS基础架构云 —— 云网络

Exploration of 6-wire SPI transmission mode

【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码

How does win11's own drawing software display the ruler?

今天去 OPPO 面试,被问麻了

MySQL之联表查询、常用函数、聚合函数
随机推荐
C # simulation lottery
80篇国产数据库实操文档汇总(含TiDB、达梦、openGauss等)
Fudan University emba2022 graduation season - graduation does not forget the original intention and glory to embark on the journey again
Recursive menu query (recursion: check yourself)
Golang review summary
【故障诊断】基于贝叶斯优化支持向量机的轴承故障诊断附matlab代码
使用 Terraform 在 AWS 上快速部署 MQTT 集群
How does win11's own drawing software display the ruler?
使用Huggingface在矩池云快速加载预训练模型和数据集
【obs】转载:OBS直播严重延迟和卡顿怎么办?
QT ListView 列表显示组件笔记
MySQL table write lock
测试框架-unittest-命令行操作、断言方法
测试框架-unittest-跳过测试
Shared lock
一文理解分布式开发中的服务治理
MySQL pessimistic lock
Communication between processes (pipeline details)
Ilssi certification | the course of Six Sigma DMAIC
pymongo保存dataframe格式的数据(insert_one, insert_many, 多线程保存)