[PyQt] Understanding QLineEdit.setReadOnly() virtuality

Phil Thompson phil at riverbankcomputing.com
Wed Jun 13 09:08:04 BST 2018


On 13/06/2018 08:22, J Barchan wrote:
> This is a question about the PyQt implementation of 
> QLineEdit.setReadOnly(),
> but it also applies to hundreds of similar functions in Qt/PyQt.
> 
> I asked elsewhere why the C++ documentation for 
> QLineEdit::setReadOnly() is
> *not* marked virtual, given that from a PyQt derived class I *can* 
> override
> QLineEdit.setReadOnly().  I was told:
> 
>> Because it's not a virtual function and yes, the documentation is 
>> correct
>> (you can check the source code)
>> 
>> Python is an entirely different beast. You can modify any element of 
>> an
>> object the way you want.
>> 
> So, is the following what is going on:
> 
>    - From C++ QLineEdit::setReadOnly() is *not* an overridable, virtual
>    function.
>    - From PyQt, there is a "wrapper" around the whole of QLineEdit, and
>    that has chosen to supply a function, which *happens* to also be 
> named
>    setReadOnly [but could have been called anything, e.g. 
> pyqtSetReadOnly],
>    which (in Python) *is* a virtual, overridable function.
>    - PyQt's QLineEdit.setReadOnly() presumably calls the base C++
>    QLineEdit::setReadOnly() to effect its work.
> 
> The big difference then is:
> 
>    - In the case of a *genuine* virtual Qt C++ function, which I have
>    chosen to override in PyQt, if Qt code *internally* calls the 
> virtual
>    function from itself it *will* hit my Python override.
>    - But in the case of this *non-*virtual QLineEdit::setReadOnly(), 
> any
>    internal Qt C++ calls to it will *not* be affected by the "override" 
> I
>    have defined from Python.
> 
> Conclusion:
> 
>    1. Does the above sound right?
>    2. Being a Python newbie: it is just sinking in, is it the case that 
> all
>    class functions are automatically overridable, because Python 
> doesn't
>    bother with virtual & override keywords?

Yes, you’ve got it.

Phil



More information about the PyQt mailing list