How to filter items along with QCheckbox from QLineEdit and without Case-insensitive . If i am using below code working but unable to get QCheckboxes along with items. Able to see items only not check boxes. Below is my example Code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(333, 373)
self.gridLayout = QtWidgets.QGridLayout(Dialog)
self.gridLayout.setObjectName("gridLayout")
self.searchLineEdit = QtWidgets.QLineEdit(Dialog)
self.searchLineEdit.setObjectName("searchLineEdit")
self.gridLayout.addWidget(self.searchLineEdit, 0, 1, 1, 1)
self.label = QtWidgets.QLabel(Dialog)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.listWidget = QtWidgets.QListWidget(Dialog)
self.listWidget.setObjectName("listWidget")
self.gridLayout.addWidget(self.listWidget, 1, 0, 1, 2)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
fruit_list = ["Apples", "pears", "oranges", "grapefruits", "strawberries"]
self.fruit_list = dict.fromkeys(fruit_list, 0)
for i in self.fruit_list:
item = QtWidgets.QListWidgetItem(i)
item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
item.setCheckState(QtCore.Qt.Unchecked)
self.listWidget.addItem(item)
self.searchLineEdit.textChanged.connect(self.filter_items)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Search"))
def filter_items(self, text):
self.listWidget.clear()
for item in self.fruit_list:
if text in item:
QtWidgets.QListWidgetItem(item, self.listWidget)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
You don't need to delete items: just hide them
def filter_items(self, text):
for i in range(self.listWidget.count()):
item = self.listWidget.item(i)
item.setHidden(text not in item.text())