[Qtscript-bindings] signals/slots overriding
Kent Hansen
khansen at trolltech.com
Tue Aug 26 11:39:14 CEST 2008
Ian Monroe wrote:
> Hey Kent and everyone,
>
> On Wed, Aug 13, 2008 at 4:02 AM, Peter Zhou <peterzhoulei at gmail.com> wrote:
>
>> how can I override a signal or a slot in a script?
>>
>
> I understand you talked to Peter Zhou and figured out that he stumbled
> upon a bug that won't be fixed until Qt 4.5. I don't quite get what
> that bug is or what he was trying to do though. :)
>
> Really the general question is: if I want to define a class in C++
> (and to use the old OOP example that maybe doesn't fit exactly) called
> Shape, how do I let the QtScript subclass Shape with Square and
> override its area() function? And have the overriden area() function
> be called by C++? The AnalogClock example seems to do this fine with
> its paintEvent.
>
Hi Ian,
It works for the classes in Qt because the generator creates special
"shell" classes for the classes it knows about (i.e. that are defined in
the typesystem XML file). When you say "new QWidget()" in a script, this
will create an instance of the C++ class QtScriptShell_QWidget. That
class knows about its "script counterpart object", and reimplements
QWidget's virtual methods to forward the call to the corresponding
script function, if there is one.
Just the fact that the function is defined as a slot is not enough,
there has to be a glue layer. So with your Shape example, you would have
to tell the generator to generate bindings for it, similar to how it's
done for the Qt classes. And the area() function has to be declared as
virtual of course, otherwise there's nothing the generator can do.
Does Amarok already have a typesystem file similar to the one plasma
uses (see
http://websvn.kde.org/trunk/playground/base/plasma/scriptengines/qtscript/plasma-bindings/generator)?
The classes in Amarok that script writers should be able to extend have
to be defined in that file, and you have to generate and import bindings
into the runtime just like you do for the classes in Qt. And as I
mentioned to Peter, there's no reason to make the C++ functions slots
when you're using the bindings generator, since you can establish a
connection from any C++ signal to any script (wrapper) function.
The bug I talked about to Peter might not be relevant in this case;
unfortunately I was in a bit of hurry and didn't manage to get the full
picture of what his code was doing. (What I thought was the problem was
that he was stumbling onto a possible conflict between the "static",
generated bindings and the dynamic/automatic QtScript QObject
signals/slots bindings.)
Regards,
Kent
More information about the Qtscript-bindings
mailing list