Filter checked checkboxes

161 Views Asked by At

By this I tried to make a minimal reproducible example of what I now have. Using QSortFilterProxyModel() to find text and a custom SortFilterProxyModel to show all the selected checkboxes.

It looks like both proxy's conflicting eachother and giving the wrong row number back. (There is also an option for adding value with a dialog and also this goes to the wrong row with the custom proxy enabled, but works without the custom proxy).

class winConfigurator(QtWidgets.QMainWindow):

    def __init__(self, data=None):
        super(winConfigurator,self).__init__()
        uic.loadUi(os.path.join(os.path.dirname(__file__), 'winConfiguratorView.ui'), self)

        self.leSearch.returnPressed.connect(self.searchField)
        self.chMatchSelected.toggled.connect(self.showSelected)
        
    def readFile(self, filename):
        self.model = TableModel([headers, newRows])

        self.proxy_model = QSortFilterProxyModel()
        self.proxy_model.setSourceModel(self.model)
        
        """
        Proxy for 'Show Selected'
        """
        self.custom_proxy_model = SortFilterProxyModel()
        self.custom_proxy_model.setSourceModel(self.proxy_model)

        self.tableView.setModel(self.custom_proxy_model)

    def searchField(self):
        self.proxy_model.setFilterFixedString(self.leSearch.text())
    
    def showSelected(self, state = None):
        self.custom_proxy_model.clearFilter()
        checkstate_items = self.model.checks.items()
        if state == True:
            self.custom_proxy_model.setFilterByCheckbox(checkstate_items)

class SortFilterProxyModel(QSortFilterProxyModel):
    def __init__(self, *args, **kwargs):
        QSortFilterProxyModel.__init__(self, *args, **kwargs)
        self.filters = {}

    def setFilterByCheckbox(self, checkstates = {}):
        self.filters = checkstates
        self.invalidateFilter()

    def clearFilter(self):
        self.filters = {}
        self.invalidateFilter()

    def filterAcceptsRow(self, source_row, source_parent):
        """
        Check if checkbox is checked and show this row
        Slow, 7 seconds for 50k rows.
        """
        try:
            values = []
            if self.filters:
                for index, is_checked in self.filters:
                    if is_checked:
                        row = index.row()
                        model = self.sourceModel()
                        if hasattr(model, 'mapToSource'):
                            index = model.index(source_row, 0, source_parent)
                            if not index.parent().isValid():
                                modelIndex = model.mapToSource(index)
                                source_row = modelIndex.row()
                                
                        if row == source_row:
                            values.append(index)
                return any(values)
            return True
        except Exception as e:
            # print(e)
            return True
0

There are 0 best solutions below