Question

I want to access the storage object inside my CoreProxy instance from a script as below:

CoreProxy proxy(core);
QScriptEngine engine;
QScriptValue coreInScript = engine.newQObject(&proxy);
engine.globalObject().setProperty("acq", coreInScript);
engine.evaluate("acq.storage.start()");

But QScriptEngine gives me this error back:

TypeError: Result of expression 'acq.storage' [undefined] is not an object.

Here is my CoreProxy class:

class CoreProxy : public QObject
{
    Q_OBJECT
private:
    Core *_core;

public:
    CoreProxy(Core *core);

    StorageProxy *storage;

public slots:
    // Public slots for javascript interactions
    QString init(QString acqId);
    QString start();
    QString stop();
};

And my StorageProxy class:

class StorageProxy : public QObject
{
    Q_OBJECT
private:
    Core *_core;

public:
    StorageProxy(Core *core);

public slots:
    // Public slots for javascript interactions
    QString start();
    QString stop();
};

I've added this in the constructor of StorageProxy, but I didn't manage to access the storage member:

this->setObjectName("storage");

Is something missing in my code?

Was it helpful?

Solution

You need to define the storage member as a property:

Q_PROPERTY(StorageProxy* storage READ storage)

For more information on what you can do with this macro, see The Property System. I've given links to Qt 4.8 documentation as you are using Qt Script, which is not actively developed; see the Scripting documentation for more information on what's available with Qt 5.1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top