1

I am using Qt 4.8 and QScriptEngine.

I want to make a C++ class usable from Javascript but I did not get it.

I already know how to expose a single object of the class.

My class looks like that:

#include <QtCore/QObject>

class Tada: public QObject
{
  Q_OBJECT
public:

  Tada(int i=0): m_i(i){};

public slots:
  int giveNumber();

private:
  int m_i;
};

on the location there I set up the script engine I can add something like

static Tada tada;
engine->globalObject().setProperty("tada", engine->newQObject(&tada));

this makes the object tadaavailable in the scripts so I can use it like

tada.giveNumber();

But that if I want to create Tada objects in the script itself like:

var mt = new Tada(34);
mt.giveNumber();

?

1 Answer 1

1

First create a constructor function like:

QScriptValue constructTada(QScriptContext * context, QScriptEngine * engine)
{
    Tada * pTada = new Tada;

    if (context->argumentCount() > 0)
    {
        // Set any properties...
        pTada->setNumber(context->argument(0).toInt32());
    }

    return engine->newQObject(pTada);
}

Then you need to put that function into the scripting environment:

QScriptEngine engine;
QScriptValue ctor = engine.newFunction(constructTada);
engine.globalObject().setProperty("Tada", ctor);
Sign up to request clarification or add additional context in comments.

5 Comments

hmm i did not get it working. since num is not a property i tried to get the object (QObject* qo = context->thisObject().toQObject();) and then qobject_cast them to Tada* (just like the marshalling functions do) but qo is allways nullptr. i got myself to a solution that looked like your else part. but i didn not had the prototype step in my code, but it seemed to work - why?
qt-project.org/doc/qt-4.8/… mentions qt-project.org/doc/qt-4.8/qscriptengine.html#newQMetaObject that shows some construction function that looks like mine. it seems that there is no difference between calling them with or without new in Script. i also can not determine any difference between using engine.newFunction with or without prototype or with or without newQMetaObject). just registering the constructer-Function and setting the global object property seems not to make any difference.
I think that if it's called with new, then Qt Script constructs a script Object. If you use a two-part constructor like in my example, then in the if block that script Object gets used. If you just always do it like in the else block, it's fine because the script Object will get garbage collected -- this now seems like the better way to me. I'm not sure what the prototype object adds.
I've simplified the example
yes, that is the way i implemented it. but iam still wondering about the prototype and metaobject things and about the java script new that seems to be needless

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.