当前位置:网站首页>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")
边栏推荐
- 城市燃气安全再拉警钟,如何防患于未“燃”?
- tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框
- 一文理解分布式开发中的服务治理
- Wechat applet does not use plug-ins, rendering videos in rich text, image adaptation, plus version
- Leetcode:528. select randomly according to the weight [ordinary random failure + prefix and dichotomy]
- [image denoising] image denoising based on bicube interpolation and sparse representation matlab source code
- Upgrade esxi6.7.0 to 7.0u3f (updated on July 12, 2022)
- Record locks
- MySQL global lock
- linux内核源码分析之页表缓存
猜你喜欢

Breakthrough in core technology of the large humanoid Service Robot Walker x

异常处理机制专题1

IAAs infrastructure cloud cloud network

用递归进行数组求和

Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool

食品安全丨无处不在的冷冻食品,你真的了解吗?

Sum arrays with recursion

优必选大型仿人服务机器人Walker X的核心技术突破

Food safety - do you really understand the ubiquitous frozen food?

Which led display manufacturer is better
随机推荐
Gap locks
Which led display manufacturer is better
02. 将参数props限制在一个类型的列表中
在 NgModule 里通过依赖注入的方式注册服务实例
MySQL显式锁
Win11自带画图软件怎么显示标尺?
2w字详解数据湖:概念、特征、架构与案例
MySQL之联表查询、常用函数、聚合函数
Talk about how to use redis to realize distributed locks?
【obs】发送前丢帧及帧优先级
01.一个更简单的方法来传递大量的props
进程之间的通信(管道详解)
MySQL check whether the table is locked
可验证随机函数 VRF
【云驻共创】探秘GaussDB如何助力工商银行打造金融核心数据
Emqx cloud update: more parameters are added to log analysis, which makes monitoring, operation and maintenance easier
pymongo保存dataframe格式的数据(insert_one, insert_many, 多线程保存)
[wechat applet] detailed explanation of applet host environment
月薪1万在中国是什么水平?答案揭露残酷的收入真相
ILSSI认证|六西格玛DMAIC的历程