当前位置:网站首页>QT5知识:字符串列表QStringListModel
QT5知识:字符串列表QStringListModel
2022-06-23 11:53:00 【无水先生】
一、QStringListModel介绍
QStringListModel是Qt提供的一个已经实现QAbstractItemModel相关接口的Model,适合于展示一系列字符串的简单视图如QListView对象和QComboBox对象。
QStringListModel提供了所有可编辑模型的标准函数,将字符串列表中的数据存储为一个一列多行的模型。换个说法,符号串的列表无法直接传入QListView对象和QComboBox,需要QStringListModel做为中间传递过程。(如下图)

使用index(int row, int column = 0, QModelIndex parent = QModelIndex())函数获取与项对应的模型索引,使用flags(QModelIndex index)获取项标志;使用data()函数读取项数据,并使用setData()写入项数据,使用rowCount()函数访问模型数据的行数。
该模型可以用现有的字符串列表来构造,或者可以用setStringList()函数来设置字符串。字符串也可以用insertRows()函数以通常的方式插入,并用removeRows()删除。字符串列表的内容可以使用stringList()函数检索。
二、使用QStringListModel作为QListView的Model
使用QStringListModel作为QListView的Model的方法非常简单,先创建QStringListModel实例对象,如果数据未存储到Model中则将数据存储到Model,然后再将QListView对象的Model设置为刚创建的Model实例。步骤如下:
2.1、 创建QStringListModel实例对象
实例对象创建有两种方法:
- 创建无数据的Model
语法为:QStringListModel(QObject parent = None)
- 创建Model同时初始化数据
语法为:QStringListModel(strings,QObject parent = None)
其中strings为要展示的字符串列表数据。
以上两种方法的实例代码:
self.model1 = QStringListModel()
strList = ["item1", "item2", "item3", "item4", "item5"]
self.model2 = QStringListModel(strList)只是以上方法创建model后,model1中无数据,而model2中有数据
2.2、初始化model中的数据
如果创建的model中无数据,可以通过几种方法初始化model中的数据:
使用setStringList(str)方法完成初始化,示例代码:
self.model1.setStringList(["item1", "item2", "item3", "item4", "item5"])
使用insertRows()、index()、setData()方法组合实现
insertRows是在model中插入空的数据记录,只是占据了数据位置,但没有实际的数据,需要通过setData来生成实际存储的数据。这种组合方法没有多大实际价值,具体方法的参数及含义在此不进行详细介绍,请参考如下示例代码:
self.model = QStringListModel()
self.model.insertRows(0,3)
for i in range(4):
index = self.model.index(i-1)
self.model.setData(index,f"item{i}")使用视图的setModel(model)方法将model和view建立连接
示例代码:
self.listView.setModel(self.model)
2.3、示例代码及运行截图
本示例代码使用先创建对象再使用setStringList方法初始化数据:

def initStringListModel(self):
strList = ["item1", "item2", "item3", "item4", "item5", "item6"]
self.model = QStringListModel()
self.model.setStringList(strList )
self.listView.setModel(self.model)完整案例:
from PyQt5.QtWidgets import QApplication,QWidget,QHBoxLayout,QListView,QAbstractItemView
from PyQt5.QtCore import QStringListModel
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
widget = QWidget()
layout = QHBoxLayout()
listModel = QStringListModel()
listView = QListView()
nameList = ["ZheDong Mao","RenLai Zhou","XiaoPing Deng","ShaoQi Liu","De Zhu","DeHuai Peng"]
listModel.setStringList(nameList)
listView.setModel(listModel)
listView.setEditTriggers(QAbstractItemView.AnyKeyPressed | QAbstractItemView.DoubleClicked)
layout.addWidget(listView)
widget.setWindowTitle("QStringListModel");
widget.setLayout(layout);
widget.show()
app.exec()运行截图:

边栏推荐
- "Dream of children's travel" in 2022, GAC Honda children's road safety charity travel entered the Northeast
- 在工作中学习的三个方法
- @Dark horse fans, haven't you received this "high temperature subsidy"?
- Go zero micro Service Practice Series (VI. cache consistency assurance)
- Invalid Navicat scheduled task
- 记录
- 【综合笔试题】30. 串联所有单词的子串
- Voice data annotation tools and platforms
- Oversampling Series II: Fourier transform and signal-to-noise ratio
- Redis 入门-第二篇-数据结构与对象-链表
猜你喜欢

2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案

凭借32量子比特!Rigetti Computing打入英国量子计算市场

年薪中位数超30万,南大AI专业首届毕业生薪资曝光

Meta称英安全法可能“扫描所有私人信息” 或侵犯隐私

全国进入主汛期,交通运输部:不具备安全运行条件的线路坚决停运!

Learning notes sweep crawler framework
![ROS observation [57]: configure arm robots to grasp things](/img/11/5545298c7b23649df09ff7f116c888.png)
ROS observation [57]: configure arm robots to grasp things

六维图剖析:中国建筑集团有限公司企业成长性分析

Analysis of six dimensional chart: analysis of enterprise growth of CSCEC

利用XtraDiagram.DiagramControl进行流程图形的绘制和控制
随机推荐
CIFAR公开第二阶段泛加拿大AI战略
公开课丨玩的就是短视频!今晚教你精准变现!
Where to find capacitance parameters!?
Runtime application self-protection (rasp): self-cultivation of application security
Go zero micro Service Practice Series (VI. cache consistency assurance)
股权转让热点:重庆建科建设工程质量检测有限公司93.75%股权转让
ESP32-C3入门教程 问题篇⑦—— fatal error: esp_bt.h: No such file or directory 找不到esp_bt.h
[cloud based co creation] overview of the IOT of Huawei cloud HCIA IOT v2.5 training series
ROS知识:rviz库librviz的结构
QT5知识:QT绘制图形
Analysis of six dimensional chart: analysis of enterprise growth of CSCEC
mysql,如何在使用存储过程计算最大值
汉源高科8路电话+1路百兆以太网RJ11电话光端机 8路PCM电话光端机
Redis 入门-第四篇-数据结构与对象-跳跃表
2022施工员-装饰方向-岗位技能(施工员)操作证考试题库模拟考试平台操作
Introduction to redis - Chapter 3 - data structures and objects - Dictionary
Linked list 5 - 234 Palindrome linked list
【无标题】2022年压力管道巡检维护试题及在线模拟考试
链表5 - 234. 回文链表
Use xtradiagram Diagramcontrol for drawing and controlling process graphics