[英]How can I display item on QListWidget when it match with the search term
I try to make a function that display item (from the list "data" below) on QlistWidget when the search term is contained in the item.当搜索词包含在项目中时,我尝试制作一个 function 在 QlistWidget 上显示项目(来自下面的列表“数据”)。
The function is below: function如下:
self.data = ["France","France","Allemagne","Serbie","Ile Maurice","States"]
self.qle = QLineEdit(self)
self.listwidget = QListWidget()
def update_display(self):
out = self.data.filter(self.qle.text(),Qt.MatchContains)
self.listwidget.addItems(out)
But it show me this error:但它告诉我这个错误:
Traceback (most recent call last):
File "InterfaceGraphique.py", line 53, in update_display
out = self.data.filter(self.qle.text(),Qt.MatchContains)
AttributeError: 'list' object has no attribute 'filter
I exactly know what is the problem but I can't try to find the solution for replace it.我完全知道问题出在哪里,但我无法尝试找到替换它的解决方案。
To compare strings against patterns, give a try to the regular expressions module要将字符串与模式进行比较,请尝试使用正则表达式模块
import re
self.data = ["France","France","Allemagne","Serbie","Ile Maurice","States"]
self.qle = QLineEdit(self)
self.listwidget = QListWidget()
def update_display(self):
for item in item in data:
if re.search(self.qle.text(),item):
self.listwidget.addItems(item)
break
re.search matches the pattern anywhere in the target string. re.search 匹配目标字符串中任意位置的模式。 If you wish to match on the beggining of the string, use re.match.
如果您希望匹配字符串的开头,请使用 re.match。 https://docs.python.org/3/library/re.html
https://docs.python.org/3/library/re.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.