0

我想通过如下脚本访问我的 CoreProxy 实例中的存储对象:

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

但是 QScriptEngine 给了我这个错误:

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

这是我的 CoreProxy 类:

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();
};

还有我的 StorageProxy 类:

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

public:
    StorageProxy(Core *core);

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

我在 StorageProxy 的构造函数中添加了这个,但我没有设法访问存储成员:

this->setObjectName("storage");

我的代码中是否缺少某些内容?

4

1 回答 1

0

您需要将storage成员定义为属性

Q_PROPERTY(StorageProxy* storage READ storage)

有关您可以使用此宏执行的操作的更多信息,请参阅属性系统。当您使用未积极开发的 Qt Script 时,我已经提供了指向 Qt 4.8 文档的链接;有关 Qt 5.1 可用功能的更多信息,请参阅脚本文档。

于 2014-02-20T20:54:54.523 回答