当前位置:网站首页>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")
边栏推荐
- Food safety - do you really understand the ubiquitous frozen food?
- 链游开发现成版 链游系统开发详细原理 链游源码交付
- Test Driven Development (TDD) online practice room | classes open on September 17
- MYSQL导入sqllite表格的两种方法
- C Music
- fastadmin tp 安装使用百度富文本编辑器UEditor
- [wechat applet] detailed explanation of applet host environment
- 【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码
- 【obs】发送前丢帧及帧优先级
- ILSSI认证|六西格玛DMAIC的历程
猜你喜欢

复旦大学EMBA同学同行专题:始终将消费者的价值放在最重要的位置

Crazy God redis notes 12

聊聊如何用 Redis 实现分布式锁?

Sum arrays with recursion

Emqx cloud update: more parameters are added to log analysis, which makes monitoring, operation and maintenance easier

0x80131500打不开微软商店的解决办法

Is the win11 dynamic tile gone? Method of restoring dynamic tile in Win 11

可验证随机函数 VRF

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

柏睿数据加入阿里云PolarDB开源数据库社区
随机推荐
【读书会第13期】+FFmpeg开源项目
easyui datagrid控件使用
Analysis and solution of data and clock mismatch delay in SPI transmission
Today, I went to oppo for an interview and was asked numbly
新增批量删除
Upgrade esxi6.7.0 to 7.0u3f (updated on July 12, 2022)
百度富文本编辑器UEditor单张图片上传跨域
测试框架-unittest-测试套件、结果输出到文件
tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框
城市燃气安全再拉警钟,如何防患于未“燃”?
[JS advanced] JS regular correlation functions and regular objects_ 02
优必选大型仿人服务机器人Walker X的核心技术突破
Quickly deploy mqtt clusters on AWS using terraform
Exploration of 6-wire SPI transmission mode
邮件的收发的展现逻辑之收件箱发件箱以及回复断链的问题
MySQL视图
Lazy loading of pictures
微信公众号开发之消息的自动回复
复旦大学EMBA同学同行专题:始终将消费者的价值放在最重要的位置
02. 将参数props限制在一个类型的列表中