搜索/过滤QListWidget的自定义小部件

时间:2018-11-05 16:03:52

标签: python pyqt pyqt5 qlistwidget

如何在QListWidget中搜索/过滤自定义窗口小部件?

例如,我有一个自定义表单小部件,

class MetadataForm(QtWidgets.QWidget):
    def __init__(self, data, parent=None):
        super(MetadataForm, self).__init__(parent)

        self.ui = Ui_MetaInfo()
        self.ui.setupUi(self)
        self.data = data
        self.keywords = data['keywords']

        self.ui.pkg_name.setText(self.data['name'])
        self.ui.pkg_version.setText(self.data['version'])
        self.ui.pkg_desc.setText(self.data['desc'])
        self.ui.pkg_author_lbl.setText(self.data['author'])
        self.ui.pkg_pubdate_lbl.setText(self.data['pub_date'])

        self.ui.install_btn.clicked.connect(self.install_pkg)

    def install_pkg(self):
        print('hello from {}'.format(self.data['name']))

我可以将它们添加到我的MainWindow

class MainWindow(QtWidgets.QDialog):
    def __init__(self, data, parent=None):
        super(MainWindow, self).__init__(parent)
        self.data = data
        self.ui = Ui_Main()
        self.ui.setupUi(self)
        self.set_catalog_data()

    def set_catalog_data(self):
        for item in self.data:
            # print(item)
            metadata = MetadataForm(item)
            print(metadata.keywords)
            lst_item = QtWidgets.QListWidgetItem()
            self.ui.catalog_list_wid.insertItem(
                self.ui.catalog_list_wid.count(),
                lst_item
            )
            self.ui.catalog_list_wid.setItemWidget(lst_item, metadata)
            lst_item.setSizeHint(metadata.sizeHint())

在我的MainWindow的用户界面中,有一个名为self.ui.search_box的QLineEdit,如何根据元数据类的keyword属性过滤自定义的QListWidgetItem

一些简单的测试数据:

test_data = [
    {
        'name': 'Foo Model',
        'version': 'v1.0.0.1',
        'desc': lorem.paragraph(),
        'author': 'Jane Doe',
        'pub_date': '10/14/2018',
        'keywords': ['foo']
    },
    {
        'name': 'Bar Model',
        'version': 'v1.0.0.1',
        'desc': lorem.paragraph(),
        'author': 'Jon Smith',
        'pub_date': '11/2/2018',
        'keywords': ['bar']
    }
]

1 个答案:

答案 0 :(得分:1)

您只需要遍历行并使用item()方法获取关键字来获取QListWidgetItem,然后使用itemWidget获取窗口小部件,使用窗口小部件获取关键字并应用过滤器,如果遇到过滤器,它将变为可见否则你必须隐藏

class MainWindow(QtWidgets.QDialog):
    def __init__(self, data, parent=None):
        super(MainWindow, self).__init__(parent)
        self.data = data
        self.ui = Ui_Main()
        self.ui.setupUi(self)
        self.ui.search_box.textChanged.connect(self.on_textChanged)
        self.set_catalog_data()

    def set_catalog_data(self):
        for item in self.data:
            # print(item)
            metadata = MetadataForm(item)
            lst_item = QtWidgets.QListWidgetItem()
            self.ui.catalog_list_wid.insertItem(
                self.ui.catalog_list_wid.count(),
                lst_item
            )
            self.ui.catalog_list_wid.setItemWidget(lst_item, metadata)
            lst_item.setSizeHint(metadata.sizeHint())

    @QtCore.pyqtSlot(str)
    def on_textChanged(self, text):
        for row in range(self.ui.catalog_list_wid.count()):
            it = self.ui.catalog_list_wid.item(row)
            widget = self.ui.catalog_list_wid.itemWidget(it)
            if text: 
                it.setHidden(not self.filter(text, widget.keywords))
            else:
                it.setHidden(False)

    def filter(self, text, keywords):
        # foo filter
        # in the example the text must be in keywords
        return text in keywords