[Interest] Problem with QAbstractProxyModel in PyQt 5.9.1

Tony Rietwyk tony at rightsoft.com.au
Mon Dec 18 00:46:49 CET 2017


Hi Jérôme,

I assume that parent.isValid is checking the parent QModelIndex by 
calling parent.model().columnCount within the DummyModel. There used to 
be a Qt utility to check that your models have the correct overrides.  
Note that the source model cannot rely on the proxy model for anything - 
it must be self contained.

Regards, Tony


On 18/12/2017 1:04 AM, Jérôme Laheurte wrote:
> 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
>
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20171218/1bd3c81b/attachment.html>


More information about the Interest mailing list