[Interest] Problem with QAbstractProxyModel in PyQt 5.9.1

Jérôme Laheurte jerome at jeromelaheurte.net
Sun Dec 17 15:04:25 CET 2017


I’m trying to use a QAbstractProxyModel (more specifically a QIdentityProxyModel) to present differently columned models for the same underlying model. So the source model doesn’t implement column-related stuff, but the proxy model does. It goes like this:

#!/usr/bin/env python3
#-*- coding: ISO-8859-1 -*-

from PyQt5 import QtCore, QtWidgets

class DummyModel(QtCore.QAbstractItemModel):
    def rowCount(self, parent):
        if parent.isValid():
            return 0
        return 10

    def parent(self, index):
        return QtCore.QModelIndex()

    def index(self, row, column, parent):
        if parent.isValid():
            return QtCore.QModelIndex()
        return self.createIndex(row, column)


class ProxyModel(QtCore.QIdentityProxyModel):
    def __init__(self, model):
        super().__init__()
        self.setSourceModel(model)

    def columnCount(self, parent):
        return 3

    def data(self, index, role):
        if index.isValid() and role == QtCore.Qt.DisplayRole:
            return 'Item #%d,%d' % (index.row(), index.column())


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        view = QtWidgets.QTreeView(self)
        view.setModel(ProxyModel(DummyModel()))
        self.setCentralWidget(view)

        self.show()
        self.raise_()


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    win = MainWindow()
    app.exec_()

Unfortunately running this gives me this on the console:

NotImplementedError: QAbstractItemModel.columnCount() is abstract and must be overridden

And nothing displays unless I define `columnCount` in the source model. So my guess is that some method in QAbstractProxyModel *other than columnCount()* calls sourceModel()->columnCount() directly instead of the proxy’s version. Is that a bug ?

Best regards
Jérôme Laheurte

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20171217/8e59755a/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 874 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20171217/8e59755a/attachment.sig>


More information about the Interest mailing list