Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtScript and prototypes
Qt 6.11 is out! See what's new in the release blog

QtScript and prototypes

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TimS
    wrote on last edited by
    #1

    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";}
    };
    @

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved