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

Phil Thompson phil at riverbankcomputing.com
Thu Jun 2 11:31:00 BST 2016


On 2 Jun 2016, at 10:53 am, Florian Bruhin <me at the-compiler.org> wrote:
> 
> * Elvis Stansvik <elvstone at gmail.com> [2016-06-02 10:36:32 +0200]:
>> Hi all,
>> 
>> From qabstractitemmodel.sip:
>> 
>> In the declaration of QAbstractTableModel:
>> 
>> private:
>>    ...
>>    virtual QModelIndex parent(const QModelIndex &child) const;
>>    virtual bool hasChildren(const QModelIndex &parent) const;
>> 
>> and in the declaration of QAbstractListModel:
>> 
>> private:
>>    ...
>>    virtual QModelIndex parent(const QModelIndex &child) const;
>>    virtual int columnCount(const QModelIndex &parent) const;
>>    virtual bool hasChildren(const QModelIndex &parent) const;
>> 
>> But these are all public functions in C++, and part of the public
>> abstract item model API.
> 
> No, they are private in C++ as well:
> https://github.com/qtproject/qtbase/blob/dev/src/corelib/itemmodels/qabstractitemmodel.h#L381-L384
> https://github.com/qtproject/qtbase/blob/dev/src/corelib/itemmodels/qabstractitemmodel.h#L407-L411
> 
> I guess the rationale is that it makes no sense to ask e.g. how many
> columns a list has?

This is an interesting read...

http://www.gotw.ca/publications/mill18.htm

>> 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...

Phil


More information about the PyQt mailing list