How and why Qt functions can be overridden
Rodrigo de Salvo Braz
rodrigobraz at gmail.com
Sun Aug 29 01:53:24 BST 2021
Correction: I used -> for a_ref->foo and b-> but that should have been .
(dot). The -> is used if we have a pointer A* or B* only.
BTW, the same things hold for pointers:
A* a_ptr = new B();
B* b_ptr = new B();
a_ptr->foo(); // original A::foo()
b_ptr->foo(); // overridden B::foo()
Rodrigo
On Sat, Aug 28, 2021 at 5:50 PM Rodrigo de Salvo Braz <rodrigobraz at gmail.com>
wrote:
> I think I can answer just a little bit of your questions.
>
> One thing is that I don't think being public or protected or private is
> important for overridden methods. They will behave the same way regardless
> as far as virtualness and being overridden are concerned. The only thing
> that changes is who can invoke them.
>
> Another thing is that, even if a method is not virtual, you can still
> override and use it. Suppose there is a class A with a method foo that is
> not virtual. If I sub-class A with a new class B with a method foo, then:
>
> A& a_ref = B();
> B b = B();
> a_ref->foo(); // calls original A::foo, because a_ref does not "know" this
> is a B object
> b->foo(); // calls overridden method B::foo, because the compiler knows b
> is a B object.
>
> Hope that helps...
>
> Rodrigo
>
> On Sat, Aug 28, 2021 at 4:33 PM Maurizio Berti <maurizio.berti at gmail.com>
> wrote:
>
>> Premise: while I can read and almost always understand C++, I don't
>> really know a lot about it.
>>
>> Subclassing and overriding of Qt classes is almost natural to me, but I
>> still don't understand some aspects about function overriding, since I'm
>> too accustomed to the "simpler" way of python subclassing.
>>
>> For instance, many widgets have an initStyleOption that cannot be
>> overridden (well, it could, but it's never called natively), while a
>> similar function can be overridden for QStyledItemDelegate.
>>
>> From my limited knowledge I can understand that I can override/overwrite
>> a function in a subclass and be sure that it's called natively by Qt if
>> it's public or virtual (like the initStyleOption of QStyledItemDelegate),
>> but this is still a very "foggy" understanding.
>>
>> For example, I can override all QItemDelegate drawing functions, except
>> for drawBackground, which is not virtual; this means that if I want to
>> implement it (because, for some reason, I don't want to use
>> QStyledItemDelegate), I need to completely port its paint method so that I
>> can finally call a possibly custom drawBackground.
>> Is it possible to make it virtual? If not (as I believe it is), then why?
>> Also, most importantly, why is it not virtual?
>>
>> I've been thinking about learning C++ for some time now, but that's a
>> long term project that I will not be able to begin in the near future. I
>> know that there's a lot of resources around the web on the topic, but it's
>> difficult to find simple explanations that could satisfy what I believe is
>> a slightly "basic" question. It's like wanting to understand the basic
>> principles about boiling liquid, without necessarily studying how molecular
>> phase transition works.
>>
>> So, I'd like to ask if anybody can help me shed some light or at least
>> point out some resources with a basic explanation about why some functions
>> cannot be overridden, both from "choice" (from the Qt and C++ perspective)
>> and from "architecture" (due to the sip binding).
>>
>>
>> Thank you,
>> Maurizio
>>
>> --
>> È difficile avere una convinzione precisa quando si parla delle ragioni
>> del cuore. - "Sostiene Pereira", Antonio Tabucchi
>> http://www.jidesk.net
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210828/f8562e7a/attachment-0001.htm>
More information about the PyQt
mailing list