考虑这个例子:
ObjectCreator
通过以下方式设置为全局属性QJSEngine
:
// ObjectCreator is exposed to engine_ env as global property
class ObjectCreator : public QObject
{
Q_OBJECT
public:
ObjectCreator(QJSEngine * engine, QObject * parent = nullptr) : QObject(parent), engine_(engine) {}
// Called from script env
Q_INVOKABLE QJSValue createObject();
private:
// engine_ is not owned
QJSEngine * engine_ = nullptr;
};
class SomeObj : public QObject
{
// ...
};
QJSValue ObjectCreator::createObject()
{
// No parent due to JavaScriptOwnership
return engine_->newQObject(new SomeObj());
}
JavaScript(engine_
从之前的代码片段中评估):
function f
{
// objectCreator is a global property of engine_
const someObj = objectCreator.createObj();
}
阅读文档我找不到这个问题的答案:
engine_->newQObject()
调用通过评估的 JavaScript 脚本调用的 C++ 函数是否有效engine_
?