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. Porting class constructor to a script
Forum Updated to NodeBB v4.3 + New Features

Porting class constructor to a script

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.4k 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.
  • 0 Offline
    0 Offline
    0...-5
    wrote on last edited by
    #1

    Hi, everybody! I have a QtScript problem.
    I have a following class to be imported to QtScript:
    @
    class CPos : public QObject, public QScriptable
    {
    Q_OBJECT
    Q_PROPERTY(float x WRITE setX READ x)
    Q_PROPERTY(float y WRITE setY READ y)
    Q_PROPERTY(float z WRITE setZ READ z)

    public:
    CPos(QObject* parent = 0) : QObject(parent) {}
    void setX(float value) {f_x = value;}
    void setY(float value) {f_y = value;}
    void setZ(float value) {f_z = value;}
    float x() const {return f_x;}
    float y() const {return f_y;}
    float z() const {return f_z;}

    private:
    float f_x, f_y, f_z;
    };

    Q_DECLARE_METATYPE(CPos*)
    @

    I wrote the following functional to be called as class-constructor:
    @
    QScriptValue Pos(QScriptContext *context, QScriptEngine *engine)
    {
    QScriptValue value = engine->newQObject(new CPos());
    value.setProperty("x", context->argument(0));
    value.setProperty("y", context->argument(1));
    value.setProperty("z", context->argument(2));
    return value;
    }
    @

    In my main functioan I do the following:
    @
    QScriptEngine engine;
    QScriptValue proto = engine.newQObject(new CPos());
    engine.setDefaultPrototype(qMetaTypeId<CPos*>(), proto);
    engine.globalObject().setProperty("Pos", engine.newFunction(Pos, proto));
    @

    But as far as I can see my script

    @
    var p = new Pos();
    @

    has no effect. Where did I go wrong?

    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