[PyKDE] class name virtual function

Phil Thompson phil at riverbankcomputing.co.uk
Fri Aug 22 10:38:01 BST 2003


On Thursday 21 August 2003 11:45 pm, Christian Bird wrote:
> Phil,
>
> I've been working on a plugin system to use python widgets in designer.  I
> was wondering if you (or anyone else reading this) could help me out.  One
> method that is called a lot from within designer is QWidget->className(). 
> All it is supposed to do is return a string containing the name of the
> QWidget. Apparently, there was some logic added to PyQt so that className
> would work when called from within python even if the user didn't implement
> it for their class.
>
> example:
>
> class SomeWidget(QWidget):
> 	pass
>
> sw = SomeWidget()
> print sw.className() #prints out "SomeWidget" and not "QWidget"
>
>
>
> Unfortunately, that same logic prevents python definitions of className in
> a widget from being called from the C++ side of things.  Other virtual
> functions seem to work just fine.  For example, if you define a new
> paintEvent method on a python Widget then when the paintEvent is called
> from the C++ side, the python method gets called.  This is true for every
> virtual function that I can find except for className.
>
> example:
> class SomeWidget(QWidget):
> 	def className(self):
> 		return "SomeWidget"
>
> sw = SomeWidget()
>
> passToCpp(sw)
>
> in c++:
> printf("className is %s\n", sw->className()); #this prints "QWidget"
>
>
> What would I need to change in sip so that the className method is handled
> the same as other virtual functions?  Thanks a lot for your help.

Make className() in qobject.sip virtual. However, there is a comment in there 
that says that this shouldn't be done - if only I could remember exactly why.

The implementation is a compromise. I suspect that the proper fix would be 
define a QObject.Q_OBJECT() method and require Python QObject sub-classes to 
call it from their ctors which would them emulate what the C++ Q_OBJECT macro 
does.

Phil




More information about the PyQt mailing list