PySide6 QSortFilterProxyModel filter proxy removes peristent editor

I have a QTableView containing a column with checkboxes. I use a delegate for the checkbox because I want to align it in the center of the column. The one which is generated via the CheckStateRole isn’t. The checkbox editor is made permanent for this column.

To sort and filter the table I use a QSortFilterProxyModel. Sorting works and filtering with wildcards also. But if I remove the wildcard filter, the checkboxes of the formerly filtered rows are not drawn. Instead the column shows 0 or 1.

The code is:

import sys
import pandas as pd
from PySide6.QtCore import Qt, Property, Signal, QAbstractTableModel
from PySide6.QtCore import QSortFilterProxyModel, QSignalBlocker
from PySide6.QtWidgets import (QCheckBox, QHBoxLayout, QStyledItemDelegate,
                               QTableView, QWidget, QApplication, QMainWindow,
                               QVBoxLayout, QLineEdit, QFrame)


class CenterCheckBox(QWidget):
    toggled = Signal(bool)

    def __init__(self, parent):
        super().__init__(parent)
        layout = QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        self.check = QCheckBox()
        layout.addWidget(self.check, alignment=Qt.AlignmentFlag.AlignCenter)
        self.check.setFocusProxy(self)
        self.check.toggled.connect(self.toggled)
        # set a 0 spacing to avoid an empty margin due to the missing text
        self.check.setStyleSheet('color: red; spacing: 0px;')
        self.setAutoFillBackground(True)

    @Property(bool, user=True)   # note the user property parameter
    def checkState(self):
        return self.check.isChecked()

    @checkState.setter
    def checkState(self, state):
        with QSignalBlocker(self.check) as blocker:
            self.check.setChecked(state)


class MyCheckboxDelegate(QStyledItemDelegate):
    def __init__(self, parent):
        super().__init__(parent)

    def createEditor(self, parent, option, index):
        check = CenterCheckBox(parent)
        check.toggled.connect(lambda: self.commitData.emit(check))
        return check

    def setModelData(self, editor, model, index):
        model.setData(index, editor.checkState)


class TableModel(QAbstractTableModel):
    def __init__(self, data):
        super().__init__()
        print(data)
        self._data = data

    def rowCount(self, index=None):
        return self._data.shape[0]

    def columnCount(self, parent=None):
        return self._data.shape[1]

    def data(self, index, role=Qt.ItemDataRole.DisplayRole):
        if index.isValid():
            if role == Qt.ItemDataRole.DisplayRole or role == Qt.ItemDataRole.EditRole:
                if self._data.columns[index.column()]=='Delete':
                    return ''
                value = self._data.iloc[index.row(), index.column()]
                return str(value)


class MyWindow(QMainWindow):
    def __init__(self, *args):
        QMainWindow.__init__(self, *args)
        self.frame = QFrame(self)
        self.verticalLayout = QVBoxLayout(self.frame)
        self.lineEdit = QLineEdit(self.frame)
        self.verticalLayout.addWidget(self.lineEdit)
        self.table_view = QTableView(self.frame)
        self.verticalLayout.addWidget(self.table_view)

        table_model = TableModel(pd.DataFrame([['1', '1'],
                                               ['0', '0'],
                                               ['2', '0']]))
        proxy_model = QSortFilterProxyModel()
        proxy_model.setSourceModel(table_model)
        self.table_view.setModel(proxy_model)
        self.table_view.setSortingEnabled(True)
        self.lineEdit.textChanged.connect(proxy_model.setFilterWildcard)

        self.table_view.setItemDelegateForColumn(1, MyCheckboxDelegate(self))
        for row in range(0, proxy_model.rowCount()):
            self.table_view.openPersistentEditor(proxy_model.index(row, 1))

        self.setCentralWidget(self.frame)


app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec())

I already searched in internet for such a behaviour, but nothing found. What I can do to see the checkboxes again after clearing the wildcard filter? May be to call openPersistentEditor again, but how to know when?

BTW:
Is there a description of the interaction between delegates, views, models and so on?
Until now I only found documentation about the classes and a very basic description
of the model view architecture.

New contributor

GePe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật