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

Elvis Stansvik elvstone at gmail.com
Thu Jun 2 11:50:24 BST 2016


2016-06-02 11:53 GMT+02:00 Florian Bruhin <me at the-compiler.org>:
> * 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?

Bah yes, my bad.

>
>> 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);

Yes sorry, I think when I tested, I must have done so through a QAIM pointer.

Elvis

>
> 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/
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt


More information about the PyQt mailing list