Qt-jambi-interest Archive, January 2008
Subclassing QWidget from Jython
Message 1 in thread
While working on an integration module in Jython for smoother Jambi
integration I ran into the QWidget sub-classing problem described in:
http://labs.trolltech.com/page/Projects/QtJambi/jython
[...]
Qt Jambi classes, like QWidget have a special constructor that takes a
QPrivateConstructor. This constructor is protected and not callable in Java
unless the user does something nasty and should he do, it will result in an
exception. The problem is that jython won't have the option of selecting
between
[...]
My work-around was to conditionally invoke QWidget.__init__ as follows.
[...]
class WrappedWidget(QWidget) :
def __init__(self, widget, parent = None) :
if parent is None :
QWidget.__init__(self)
else :
QWidget.__init__(self, parent)
[...]
With None (i.e. null), Jython chooses the wrong constructor--so you can
emulate "super((QWidget) null)" by just calling the zero argument
constructor. It is not ambiguous when the parameter is not None.
Hopefully this helps others that have run into this situation.
Another, possibly useful, tidbit in Jython that helped me a bit when working
with Jython types derived from QWidget especially when loading a Qt Designer
.jui file with QUiLoader was to override "__getattr__(self, name)" to
execute "QObject.findChild(java.lang.Object, name)" for attribute lookup.
This makes things like myWidget.aTextEdit automatically work if there is a
child named "aTextEdit" in the QWidget.
Cheers,
Almann
--
[ signature omitted ]
Message 2 in thread
Almann T. Goo wrote:
Hi Almann,
These are two pretty neat tricks. Thanks for sharing them with us!
-
Gunnar
> While working on an integration module in Jython for smoother Jambi
> integration I ran into the QWidget sub-classing problem described in:
>
> http://labs.trolltech.com/page/Projects/QtJambi/jython
> [...]
> Qt Jambi classes, like QWidget have a special constructor that takes a
> QPrivateConstructor. This constructor is protected and not callable in Java
> unless the user does something nasty and should he do, it will result in an
> exception. The problem is that jython won't have the option of selecting
> between
> [...]
>
> My work-around was to conditionally invoke QWidget.__init__ as follows.
>
> [...]
> class WrappedWidget(QWidget) :
> def __init__(self, widget, parent = None) :
> if parent is None :
> QWidget.__init__(self)
> else :
> QWidget.__init__(self, parent)
> [...]
>
> With None (i.e. null), Jython chooses the wrong constructor--so you can
> emulate "super((QWidget) null)" by just calling the zero argument
> constructor. It is not ambiguous when the parameter is not None.
>
> Hopefully this helps others that have run into this situation.
>
> Another, possibly useful, tidbit in Jython that helped me a bit when working
> with Jython types derived from QWidget especially when loading a Qt Designer
> .jui file with QUiLoader was to override "__getattr__(self, name)" to
> execute "QObject.findChild(java.lang.Object, name)" for attribute lookup.
> This makes things like myWidget.aTextEdit automatically work if there is a
> child named "aTextEdit" in the QWidget.
>
> Cheers,
> Almann