如何使用 QCheckBox 过滤 QListWidget 中的项目

How to filter items in QListWidget with QCheckBox

如何在不区分大小写的情况下从 QLineEdit 中连同 QCheckbox 一起过滤项目。 如果我使用下面的代码工作但无法获得 QCheckboxes 和项目。只能看到项目而不是复选框。 下面是我的示例代码:

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_())

您不需要删除项目:只需隐藏它们

def filter_items(self, text):
    for i in range(self.listWidget.count()):
        item = self.listWidget.item(i)
        item.setHidden(text not in item.text())