[PyKDE] PyQt and virtual functions.

Phil Thompson phil at river-bank.demon.co.uk
Thu Apr 13 20:48:58 BST 2000


mgeras at ticon.net wrote:
> 
> Hey,
> 
> How exactly does the handling of virtual functions work for PyQt and
> PyKDE?  I am trying to instance a class and override mousePressedEvent and
> some other events and it seems as though they still call the super-classes
> functions.  Should it work this way?  I would much prefer to have to call
> the super-class functions by hand (if and when I want to).  Maybe it's
> already done?  I just have to enable it?
> 
> Thanks,
> Matt

For each Qt/KDE class that contains a virtual function in the class
hierachy, SIP generates a wrapper class which contains a "catcher"
function for each of the virtuals.  When an instance of a class is
created from Python it is actually an instance of the wrapper class that
it created.

For example...

class C {
public:
	virtual void foo(int);
}

...SIP generates...

class sipC : public C {
public:
	void foo(int);
};

...and "i = C()" in Python gets translated to "i = new sipC" in C++.

When C::foo() is called from with the C++ library somewhere, it actually
calls sipC::foo() which knows about the corresponding Python instance. 
It then searches the Python instance for a Python method called foo.  If
it finds one, it converts its arguments to Python objects and evaluates
the Python method, converts any result back to C++ types and returns. 
If it can't find a Python method it calls C::foo().  For efficiency, the
search is only done once (for a particular instance and method) and the
result cached.

Phil




More information about the PyQt mailing list