Python3PyQt5基础一实现QListView搜索过滤问题
目录
Python3+PyQt5基础(一)实现QListView搜索过滤问题
1.本文要解决的问题:通过模糊查询检索过滤QListView中的内容
2.解决方法:
1)通过python的列表list数据转换为QStringListModel
self.strlist = ['aa1','ab1','aab1', 'bcb2', 'cc3', 'dd4', 'ee5', 'fff6', 'ggggg6']
def initData(self):
self.model = QStringListModel(self)
self.model.setStringList(self.strlist)
self.listView.setModel(self.model)
self.lineEdit.textChanged[str].connect(self.searchdo)
self.listView.clicked.connect(self.showmess)
2)字符串对list检索过滤,并将新的QStringList赋值给QStringListModel
def searchdo(self,str):
print(str)
self.tmplist = []
for ttstr in self.strlist:
if str in ttstr:
self.tmplist.append(ttstr)
print(self.tmplist)
self.model.setStringList(self.tmplist)
总结:重点在于1)如何将python的列表类型变为QStringListModel
2)如何实现PyQt5的QListView的过滤
下一篇将实现:
项目源码下载说明:两篇博文的源码集中在一个项目中
源码下载地址: [https://download.csdn.net/download/gui818/85293181
“
)