简体   繁体   English

如何将QSortFilterProxyModel的setFilterRegExp()与filterAcceptsRow()一起使用

[英]How to use QSortFilterProxyModel's setFilterRegExp() along with filterAcceptsRow()

While implementing QSortFilterProxyModel (to be used with QTableView ) I have overridden the proxy's filterAcceptsRow() method by defining in it some custom logic (on how the items get filtered). 在实现QSortFilterProxyModel (与QTableView一起使用)时,我通过在其中定义一些自定义逻辑(关于如何过滤项目filterAcceptsRow()覆盖了代理的filterAcceptsRow()方法。 Now it seems that overriding a proxy's "default" filterAcceptsRow() conflicts with a build-in proxy's functionality built around .setFilterRegExp() 现在看来,重写代理的“默认” filterAcceptsRow()与围绕.setFilterRegExp()构建的内置代理的功能发生冲突。

mySearchField=QLineEdit()
QObject.connect(mySearchField, SIGNAL("textChanged(QString)"), myProxyModel.setFilterRegExp)

While typing into mySearchField I do see that the proxy's filterAcceptsRow() is being constantly called . 键入mySearchField我确实看到代理的filterAcceptsRow()不断被调用。 But since I have overrode its filterAcceptsRow() method with a custom logic no filtering on a text typed into the lineedit happens. 但是,由于我已使用自定义逻辑覆盖了它的filterAcceptsRow()方法,因此不会对输入到lineedit中的文本进行过滤。

Question: Is it possible to keep both functionality: a simplicity of using proxy's built-in .setFilterRegExp() with custom defined filterAcceptsRow() method? 问题:是否可以同时保留两种功能:通过自定义定义的filterAcceptsRow()方法使用代理的内置.setFilterRegExp()的简便性?

In a QSortFilterProxyModel 's constructor a self.searchText variable is declared first: QSortFilterProxyModel的构造函数中,首先声明self.searchText变量:

class TaskProxyModel(QSortFilterProxyModel):
    def __init__(self):
        super(TaskProxyModel, self).__init__()
        self.searchText=None

Defining a proxy's setSearchText() custom setter to be linked to lineedit ( lineedit is used to enter a search keyword): 定义要链接到lineedit的代理的setSearchText()自定义设置器( lineedit用于输入搜索关键字):

class MyProxyModel(QSortFilterProxyModel):
    def __init__(self):
        super(MyProxyModel, self).__init__()
        self.searchText=None

    def setSearchText(self, arg=None):
        self.searchText=arg
        self.reset()

Linking the lineedit to a proxy's setSearchText() setter-method: lineedit链接到代理的setSearchText() setter方法:

myProxy=MyProxyModel()
lineEdit=QLineEdit()
lineEdit.textChanged.connect(myProxy.setSearchText)

At the beginning of proxy's filterAcceptsRow() checking if self.searchText is not None. 在代理的filterAcceptsRow()开头,检查self.searchText是否不为None。 If not then do some logic: 如果没有,请执行一些逻辑:

class MyProxyModel(QSortFilterProxyModel):
    def __init__(self):
        super(MyProxyModel, self).__init__()
        self.searchText=None

    def filterAcceptsRow(self, rowProc, parentProc):
        sourceModel=self.sourceModel()      
        indexProc=sourceModel.index(rowProc, 0, parentProc)
        node=sourceModel.data(indexProc, Qt.UserRole).toPyObject()

        if self.searchText:
            nodeName=node.getName()
            if nodeName and not str(self.searchText).lower() in nodeName.lower():
                return False 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM