[PyQt] Public QAbstractItemModel methods (wrongfully?) made private in QAbstract(Table|List)Model?

Florian Bruhin me at the-compiler.org
Thu Jun 2 11:43:23 BST 2016


* Phil Thompson <phil at riverbankcomputing.com> [2016-06-02 11:31:00 +0100]:
> >> This leads to things like:
> >> 
> >> [estan at pyret ~]$ cat test.py
> >> from PyQt5.QtCore import QStringListModel
> >> model = QStringListModel()
> >> model.hasChildren()
> >> [estan at pyret ~]$ python test.py
> >> Traceback (most recent call last):
> >>  File "test.py", line 3, in <module>
> >>    model.hasChildren()
> >> TypeError: QAbstractListModel.hasChildren() is a private method
> >> 
> >> while the same works fine in C++.
> > 
> > It does not for me:
> > 
> > test.cpp: In function 'int main(int, char**)':
> > test.cpp:17:25: error: 'virtual bool QAbstractListModel::hasChildren(const QModelIndex&) const' is private within this context
> >     qsl->hasChildren(idx);
> 
> However the C++ base class should be able to invoke a Python re-implementation. The "private" means that a sub-class cannot call the super-class implementation. This is a problem because there is then no way to fallback to the C++ implementation if there is no Python re-implementation.
> 
> So - I don't think it can be made to work, so I'm inclined not to change anything...

FWIW, the C++ implementation is trivially reimplemented:
https://github.com/qtproject/qtbase/blob/dev/src/corelib/itemmodels/qabstractitemmodel.cpp#L3536-L3539
https://github.com/qtproject/qtbase/blob/dev/src/corelib/itemmodels/qabstractitemmodel.cpp#L3568-L3576

For the table model it's a little bit more, but still not much:
https://github.com/qtproject/qtbase/blob/dev/src/corelib/itemmodels/qabstractitemmodel.cpp#L3387-L3390
https://github.com/qtproject/qtbase/blob/dev/src/corelib/itemmodels/qabstractitemmodel.cpp#L3400-L3405

It's what I ended up doing for PySide as it has no sip.cast here:
https://github.com/pytest-dev/pytest-qt/pull/63/files#diff-7699e7321b4ed40b61234778d95f5ca8R589

Florian

-- 
http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072 | http://the-compiler.org/pubkey.asc
         I love long mails! | http://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160602/65487d52/attachment.sig>


More information about the PyQt mailing list