QJSEngine中的动态c ++对象

时间:2016-06-09 07:06:30

标签: c++ qt qml qt5

我无法弄清楚如何在QJSEngine

中组织动态创建c ++对象

我尝试这样做:

的main.cpp

#include <QCoreApplication>
#include <QQmlEngine>
#include <QObject>
#include <QQmlComponent>
#include <QtQml>
#include <QQmlEngine>
#include "js_object.h"

Q_DECLARE_METATYPE(Js_Object*)

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QJSEngine myEngine;
    qmlRegisterType<Js_Object>("Js_Object", 1, 0, "Js_Object");
    QJSValue v=myEngine.evaluate("import Js_Object 1.0;\n var t= new  Js_Object() ;");
    qDebug()<<v.toString();
    return a.exec();
}

JsObject.h

#ifndef JS_OBJECT_H
#define JS_OBJECT_H

#include <QObject>
class Js_Object : public QObject
{
    Q_OBJECT
public:
    explicit Js_Object(QObject *parent = 0) : QObject(parent) {
        qDebug()<<"Js_Object::Js_Object(QObject *parent) : QObject(parent)";
}
};
#endif // JS_OBJECT_H

这个例子编译,bun控制台打印:

"SyntaxError: Syntax error"

我做错了什么?

QScriptEngine允许这样做,但在QT5.5中不推荐使用QScriptEngine, 尝试使用QJSEngine

0 个答案:

没有答案