[Qtscript-bindings] QScriptValue::isFunction() in 4.5

Kent Hansen khansen at trolltech.com
Tue Jan 6 16:03:18 CET 2009

syntheticpp at gmx.net wrote:
>> Right, QObject doesn't implement hasInstance(). Of the built-in object
>> types only Function implements hasInstance(). With Qt 4.5 you can
>> implement it for custom types (see
>> http://doc.trolltech.com/main-snapshot/qscriptclass.html#extension).
>> It sounds like your script is applying the instanceof operator to an
>> object created with QScriptEngine::newQObject(). I'm sorry that it
>> breaks now, but it should never have been working in the first place.
>> :-( Maybe you can share some more information on what the script is
>> doing, then it's easier to suggest a proper solution.
> OK, the patch is wrong. And you are right, I use newQObject() with a 
> QScriptable object as constructor.

Why is the QScriptable object used as a constructor? You can't actually
call it to construct objects.

> It seems I have too redesign this code and to use a simple creator function, or
> do you know a better way. (I've tried so set the prototype of the ctor object
> to Function.prototype, but this doesn't help either)
> Peter

Right, setting the prototype won't help because the object has to
implement [[HasInstance]] before any other conditions (i.e. the
prototype object) are considered.
If you really want to keep the current design (i.e. have the actual
script work as before) you could subclass QScriptClass, implement the
HasInstance extension, and have the QScriptClass instance be a proxy to
the QScriptable object. Not something I would ordinarily do, though. ;-)
If you don't want to change the constructor to be a normal function,
maybe the QScriptable can have an isA() function that scripts should use
instead of the instanceof operator?


More information about the Qtscript-bindings mailing list