QtScript and prototypes
-
Referring to the ByteArrayClass constructore code in CustomClass example, can someone tell me what this does?
@proto.setPrototype(global.property("Object").property("prototype"));@If I remove it from the CustomClass example it seems to make no difference to execution output.
If I include it in my engine setup code, the defaultPrototype I set is no longer used once I do a set property action (i.e. WRITE xxxx in Q_PROPERTY)
I hope the code is reasonably self explanatory, the essence being someone can do the JS below. The prototype is no longer used for 'a' once I do the set 'a.skin =...'. If I remove the setPrototype above it works fine.
@var a = new Document();
print(a.skin);
a.skin = 'skin';
print(a.skin);@I'll be frank - I've got very little idea of what I'm doing here, just referring to examples and googling documentation etc to try things that work.
This is always pushing my limits big time on JS prototypes..Simplified code:
// Setup
@
#include "documentprototype.h"QScriptValue documentToScriptValue(QScriptEngine *engine, const QSharedPointer<Document> &a)
{
return engine->newVariant(qVariantFromValue(a));
}void documentFromScriptValue(const QScriptValue &obj, QSharedPointer<Document> &a)
{
a = qvariant_cast<QSharedPointer<Document> >(obj.toVariant());
}QScriptValue construct(QScriptContext *ctx, QScriptEngine *engine)
{
if (!ctx->isCalledAsConstructor())
return ctx->throwError(QScriptContext::SyntaxError, "please use the 'new' operator");QSharedPointer<Document> p(NewDocument());
return engine->newVariant(qVariantFromValue(p));
}void Plugin::SetupScriptEngine(QScriptEngine engine)
{
qScriptRegisterMetaType<QSharedPointer<Document> >(engine, documentToScriptValue, documentFromScriptValue);
static DocumentPrototype documentPrototype;
static QScriptValue proto = engine->newQObject(&documentPrototype, QScriptEngine::QtOwnership);
// proto.setPrototype(engine->globalObject().property("Object").property("prototype"));
static QScriptValue ctor = engine->newFunction(construct, proto);
engine->globalObject().setProperty("Document", ctor);
engine->setDefaultPrototype(qMetaTypeId<QSharedPointer<Document> >(), proto);
engine->setDefaultPrototype(qMetaTypeId<Document>(), proto);}
@
// Prototype header
@
Q_DECLARE_METATYPE(Document*)
Q_DECLARE_METATYPE(QSharedPointer<Document>)
class DocumentPrototype : public QObject, public QScriptable
{
Q_OBJECT
Q_PROPERTY(QString skin READ skin WRITE setSkin)
Q_PROPERTY(QString version READ version WRITE setVersion)
Q_PROPERTY(int height READ height WRITE setHeight FINAL)
private:
Document* thisDocument() { return qscriptvalue_cast<QSharedPointer<Document> >(thisObject()); }
public:
DocumentPrototype(QObject *parent = 0) : QObject(parent) { }
~DocumentPrototype() { qDebug() << "DocumentPrototype destructor"; }QString skin() { return thisDocument()->GetSkin().c_str(); }
void setSkin(const QString &v) { qDebug("setSkin"); thisDocument()->SetSkin(v.toStdString().c_str()); }
QString version() { return thisDocument()->GetVersion().c_str(); }
void setVersion(const QString &v) { thisDocument()->SetVersion(v.toStdString().c_str()); }
int height() { return thisDocument()->GetHeight(); }
void setHeight(int v) { thisDocument()->SetHeight(v); }
public slots:
QString toString() {return "Document";}
};
@