带有自定义类的 QtScript instanceof 会引发原型相关的错误

发布于 2024-12-25 14:03:54 字数 11805 浏览 3 评论 0原文

我有一个 Qt 项目,它使用 QtScript 模块使我的应用程序的某些组件可编写脚本。

在多次尝试使现有的类直接在 QtScript 中使用之后,我选择使用继承 QObject 和 QScriptable 的包装类(主要是因为我有一些非 qobject 派生类,它们被其他继承或不继承 QObject 的类继承,并且它是因此我不可能统一对待我所有的课程)。

我现在尝试使用基于原型的继承。

我必须创建具有相应包装器 Wrapper_DrawableWrapper_MeshDrawableDrawableMeshDrawable 类。 MeshDrawable继承DrawableWrapper_MeshDrawable继承Wrapper_Drawable

我让脚本引擎知道这两个类(m_scriptEngine):

Wrapper_Drawable* wrapper_drawable = new Wrapper_Drawable();
QScriptValue wrapper_drawable_obj = m_scriptEngine->newQObject(wrapper_drawable);
m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_Drawable*>(),
                                    wrapper_drawable_obj);

Wrapper_MeshDrawable* wrapper_meshDrawable = new Wrapper_MeshDrawable();
QScriptValue wrapper_meshDrawable_obj = m_scriptEngine->newQObject(wrapper_meshDrawable);
m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_MeshDrawable*>(),
                                    wrapper_meshDrawable_obj);

如果我没记错的话,文档说脚本引擎会将 Wrapper_Drawable 的原型应用于类型 Wrapper_MeshDrawable 因为它们在 C++ 中具有继承关系。

我有一个方法 Wrapper_Drawable::drawables() ,它返回 Wrapper_Drawable 的所有子级(实际上是包装在 Wrapper_Drawable 中的 Drawable 的子级) >)。由于 Drawable 是一个抽象类,因此所有子级实际上都是 MeshDrawable

因为我希望用户“相信”他使用 Drawable 而不是 Wrapper_Drawable,所以我声明:

m_scriptEngine->globalObject().setProperty("Drawable", wrapper_drawable_obj);

其中wrapper_drawable_obj 已在上面声明。


我想测试引擎是否能够识别 Wrapper_MeshDrawable,即使它们是在 Wrapper_Drawable 数组中声明的。

curChildren = myDrawable.drawables()[0];
print(curChildren instanceof Drawable);

遗憾的是,instanceof 抛出了这个错误:

TypeError: instanceof called on an object with an invalid prototype property.

如果我尝试执行 curChildren instanceof Number 它会返回 false,所以我猜问题与 Wrapper_Drawable 有关,但我无法弄清楚它到底是什么。

预先感谢您的善意帮助。

编辑>我添加了ScriptManager(处理脚本引擎并声明各种类型)、Wrapper_Drawable(只有一些方法,否则我无法发布此消息)和Wrapper_MeshDrawable的代码。

我还仔细检查了 Wrapper_Drawable 对象上的 instanceof,对于 Number 返回 false,对于 Object 返回 true,并使用 Drawable 抛出上述错误(与名称不同,这是一个 Wrapper_Drawable 对象) )。

因此,有 Wrapper_Drawable 类的代码:

#ifndef WRAPPER_DRAWABLE_H
#define WRAPPER_DRAWABLE_H

#include <QObject>
#include <QScriptable>

#include "../drawable.h"

class Wrapper_Drawable : public QObject, public QScriptable
{
    Q_OBJECT
public:
    explicit Wrapper_Drawable(Drawable* drawable = 0, QObject *parent = 0);

    virtual Drawable* drawable() const;
signals:

public slots:
    QScriptValue visible() const;

    virtual QScriptValue loadData();

    QScriptValue rotate();
    QScriptValue translate();
    QScriptValue scale();

    QScriptValue modelMatrix() const;
    QScriptValue completeModelMatrix() const;

    QScriptValue name() const;
    QScriptValue setName();

    QScriptValue shaderProgramName() const;
    QScriptValue setShaderProgramName();

    QScriptValue row() const;

        QScriptValue childCount() const;

    QScriptValue child() const;
    QScriptValue appendChild();
    QScriptValue insertChildren();
    QScriptValue insertChild();
    QScriptValue removeChildren();

    QScriptValue children() const;
    QScriptValue visibleChildren() const;

    QScriptValue parent() const;
    QScriptValue setParent();

protected:
    Drawable* m_drawable;
};

Q_DECLARE_METATYPE(QList<Wrapper_Drawable*>)
Q_DECLARE_METATYPE(Wrapper_Drawable*)

#endif // WRAPPER_DRAWABLE_H

CPP:

#include "wrapper_drawable.h"

Wrapper_Drawable::Wrapper_Drawable(Drawable *drawable, QObject *parent) :
    QObject(parent), m_drawable(drawable)
{
}

Drawable* Wrapper_Drawable::drawable() const {
    return m_drawable;
}

QScriptValue Wrapper_Drawable::removeChildren() {
    Wrapper_Drawable* wrapper_drawable = qscriptvalue_cast<Wrapper_Drawable*>(context()->thisObject());

    if(!wrapper_drawable)
        return context()->throwError(QScriptContext::TypeError, "Drawable.insertChildren: this object is not a Drawable");

    Drawable* drawable = wrapper_drawable->drawable();

    if(!drawable)
        return context()->throwError(QScriptContext::TypeError, "Drawable.insertChildren: no Drawable wrapped");

    if(argumentCount() != 1)
        return context()->throwError(QScriptContext::SyntaxError, "Drawable.insertChildren takes exactly 1 argument");

    // TODO: maybe allow multiple arguments

    if(!argument(0).isNumber())
        return context()->throwError(QScriptContext::TypeError, "Drawable.insertChildren: argument 0 should be a number");

    unsigned int position = argument(0).toNumber();

    if(!argument(1).isNumber())
        return context()->throwError(QScriptContext::TypeError, "Drawable.insertChildren: argument 1 should be a number");

    unsigned int count = argument(1).toNumber();

    return engine()->toScriptValue(drawable->removeChildren(position, count));
}

QScriptValue Wrapper_Drawable::visibleChildren() const {
    Wrapper_Drawable* wrapper_drawable = qscriptvalue_cast<Wrapper_Drawable*>(context()->thisObject());

    if(!wrapper_drawable)
        return context()->throwError(QScriptContext::TypeError, "Drawable.children: this object is not a Drawable");

    Drawable* drawable = wrapper_drawable->drawable();

    if(!drawable)
        return context()->throwError(QScriptContext::TypeError, "Drawable.children: no Drawable wrapped");

    if(argumentCount() > 0)
        return context()->throwError(QScriptContext::SyntaxError, "Drawable.children does not take any argument");

    QList<Drawable*> drawables = drawable->visibleChildren();
    QList<Wrapper_Drawable*> wrapper_drawables;
    for(QList<Drawable*>::const_iterator it = drawables.constBegin(), end = drawables.constEnd();
        it != end; ++it)
    {
        wrapper_drawables.append(new Wrapper_Drawable(*it));
    }

    return engine()->toScriptValue(wrapper_drawables);
}

MeshDrawable:

#ifndef WRAPPER_MESHDRAWABLE_H
#define WRAPPER_MESHDRAWABLE_H

#include "wrapper_drawable.h"

#include "../meshdrawable.h"

class Wrapper_MeshDrawable : public Wrapper_Drawable
{
    Q_OBJECT
public:
    Wrapper_MeshDrawable(MeshDrawable* meshDrawable = 0, QObject *parent = 0);

    virtual MeshDrawable* drawable() const;

public slots:
    QScriptValue addTri();
    QScriptValue addQuad();
    QScriptValue setSmoothing();
};

Q_DECLARE_METATYPE(Wrapper_MeshDrawable*)

#endif // WRAPPER_MESHDRAWABLE_H

CPP:

#include "wrapper_meshdrawable.h"

Wrapper_MeshDrawable::Wrapper_MeshDrawable(MeshDrawable *meshDrawable, QObject *parent) :
    Wrapper_Drawable(meshDrawable, parent)
{
}

MeshDrawable* Wrapper_MeshDrawable::drawable() const {
    return static_cast<MeshDrawable*>(Wrapper_Drawable::drawable());
}

QScriptValue Wrapper_MeshDrawable::addTri() {

}

QScriptValue Wrapper_MeshDrawable::addQuad() {

}

QScriptValue Wrapper_MeshDrawable::setSmoothing() {

}

最后是 ScriptManager (我在其中向脚本引擎声明各种类型):

#ifndef SCRIPTMANAGER_H
#define SCRIPTMANAGER_H

#include <QtScript/QScriptEngine>
#include <QtScriptTools/QScriptEngineDebugger>
#include <QtScriptTools/QtScriptTools>
#include <QStringList>
#include <QObject>

#include "utility.h"

class ScriptManager : public QObject {
    Q_OBJECT
public:
    ScriptManager();

public slots:
    QString interprete(QString command);

private:
    void initializeFunctions();

    QScriptEngine* m_scriptEngine;

    QScriptEngineDebugger* m_scriptEngineDebugger;
};

#endif // SCRIPTMANAGER_H

CPP

#include "scriptmanager.h"
#include "scenegraph.h"

#include "meshdrawable.h"
#include "objdrawable.h"

#include <QScriptValueIterator>

#include "wrappers/wrapper_camera.h"
#include "wrappers/wrapper_cameramanager.h"
#include "wrappers/wrapper_drawable.h"
#include "wrappers/wrapper_meshdrawable.h"
#include "wrappers/wrapper_drawablemanager.h"
#include "wrappers/wrapper_scenegraph.h"
#include "wrappers/wrapper_shadermanager.h"

QString ScriptManager::returnString = QString();

ScriptManager::ScriptManager() : m_scriptEngine(new QScriptEngine())
{
    initializeFunctions();
}

void ScriptManager::initializeFunctions() {
    qScriptRegisterQObjectMetaType<QGLShaderProgram*>(m_scriptEngine);

    qScriptRegisterSequenceMetaType<QList<Wrapper_Drawable*> >(m_scriptEngine);

    QScriptValue function_ls = m_scriptEngine->newFunction(scriptFunction_ls);
    m_scriptEngine->globalObject().setProperty("ls", function_ls);

    QScriptValue function_print = m_scriptEngine->newFunction(scriptFunction_print);
    m_scriptEngine->globalObject().setProperty("print", function_print);

//    m_scriptEngine->setDefaultPrototype(qMetaTypeId<Observer*>(),
//                                        Observer::getDefaultPrototype(m_scriptEngine));

    Wrapper_Drawable* wrapper_drawable = new Wrapper_Drawable();
    QScriptValue wrapper_drawable_obj = m_scriptEngine->newQObject(wrapper_drawable);
    m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_Drawable*>(),
                                        wrapper_drawable_obj);
    m_scriptEngine->globalObject().setProperty("Drawable", wrapper_drawable_obj);

    Wrapper_MeshDrawable* wrapper_meshDrawable = new Wrapper_MeshDrawable();
    QScriptValue wrapper_meshDrawable_obj = m_scriptEngine->newQObject(wrapper_meshDrawable);
    m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_MeshDrawable*>(),
                                        wrapper_meshDrawable_obj);
    m_scriptEngine->globalObject().setProperty("MeshDrawable", wrapper_meshDrawable_obj);

    Wrapper_Camera* wrapper_camera = new Wrapper_Camera();
    m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_Camera*>(),
                                        m_scriptEngine->newQObject(wrapper_camera));

    Wrapper_CameraManager* wrapper_cameraManager = new Wrapper_CameraManager();
    m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_CameraManager*>(),
                                        m_scriptEngine->newQObject(wrapper_cameraManager));

    Wrapper_DrawableManager* wrapper_drawableManager = new Wrapper_DrawableManager();
    m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_DrawableManager*>(),
                                        m_scriptEngine->newQObject(wrapper_drawableManager));

    Wrapper_SceneGraph* wrapper_sceneGraph = new Wrapper_SceneGraph(SceneGraph::instance());
    m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_SceneGraph*>(),
                                        m_scriptEngine->newQObject(wrapper_sceneGraph));

    QScriptValue object_sceneGraph = m_scriptEngine->newQObject("sceneGraph", wrapper_sceneGraph);
    m_scriptEngine->globalObject().setProperty("sceneGraph", object_sceneGraph);

    Wrapper_ShaderManager* wrapper_shaderManager = new Wrapper_ShaderManager();
    m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_ShaderManager*>(),
                                        m_scriptEngine->newQObject(wrapper_shaderManager));

    m_scriptEngineDebugger = new QScriptEngineDebugger();
    m_scriptEngineDebugger->attachTo(m_scriptEngine);
}

I have a Qt project which uses the QtScript module to make some components of my application scriptable.

After several attempts at making the existing classes directly usable in QtScript, I chose to go with wrapper classes which inherit QObject and QScriptable (mainly because I have some non-qobject derived classes which are inherited by others which either inherit QObject or not and it was thus impossible for me to treat all my classes uniformly).

I'm now trying to use prototype-based inheritance.

I have to classes Drawable and MeshDrawable which have corresponding wrappers Wrapper_Drawable and Wrapper_MeshDrawable. MeshDrawable inherits Drawable and Wrapper_MeshDrawable inherits Wrapper_Drawable.

I make both classes known to the script engine (m_scriptEngine):

Wrapper_Drawable* wrapper_drawable = new Wrapper_Drawable();
QScriptValue wrapper_drawable_obj = m_scriptEngine->newQObject(wrapper_drawable);
m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_Drawable*>(),
                                    wrapper_drawable_obj);

Wrapper_MeshDrawable* wrapper_meshDrawable = new Wrapper_MeshDrawable();
QScriptValue wrapper_meshDrawable_obj = m_scriptEngine->newQObject(wrapper_meshDrawable);
m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_MeshDrawable*>(),
                                    wrapper_meshDrawable_obj);

If I'm not mistaken, the doc says that the script engine will apply the prototype of Wrapper_Drawable to objects of type Wrapper_MeshDrawable as they have an inheritance relationship in C++.

I have a method Wrapper_Drawable::drawables() which returns all the children of a the Wrapper_Drawable (actually the children of the Drawable wrapped in the Wrapper_Drawable). As Drawable is an abstract class, all children are actually MeshDrawables.

As I want the user to "believe" that he uses Drawables and not Wrapper_Drawables, I've declared:

m_scriptEngine->globalObject().setProperty("Drawable", wrapper_drawable_obj);

where wrapper_drawable_obj was declared above.


I wanted to test if the engine recognizes Wrapper_MeshDrawables even if they're declared in an array of Wrapper_Drawables.

curChildren = myDrawable.drawables()[0];
print(curChildren instanceof Drawable);

Sadly, instanceof throws this error:

TypeError: instanceof called on an object with an invalid prototype property.

if I try to do curChildren instanceof Number it returns false so I guess the problem is related to Wrapper_Drawable but I cannot figure out what it exactly is.

Thank you in advance for your kind help.

EDIT > I've added the code of ScriptManager (handles the script engine and declares the various types), Wrapper_Drawable (only some methods, otherwise I cannot post this message) and Wrapper_MeshDrawable.

I've also double-checked that instanceof on a Wrapper_Drawable object, returns false with Number, true with Object and throws the above-mentionned error with Drawable (which is, unlike the name says, a Wrapper_Drawable object).

So, there's the Wrapper_Drawable class' code:

#ifndef WRAPPER_DRAWABLE_H
#define WRAPPER_DRAWABLE_H

#include <QObject>
#include <QScriptable>

#include "../drawable.h"

class Wrapper_Drawable : public QObject, public QScriptable
{
    Q_OBJECT
public:
    explicit Wrapper_Drawable(Drawable* drawable = 0, QObject *parent = 0);

    virtual Drawable* drawable() const;
signals:

public slots:
    QScriptValue visible() const;

    virtual QScriptValue loadData();

    QScriptValue rotate();
    QScriptValue translate();
    QScriptValue scale();

    QScriptValue modelMatrix() const;
    QScriptValue completeModelMatrix() const;

    QScriptValue name() const;
    QScriptValue setName();

    QScriptValue shaderProgramName() const;
    QScriptValue setShaderProgramName();

    QScriptValue row() const;

        QScriptValue childCount() const;

    QScriptValue child() const;
    QScriptValue appendChild();
    QScriptValue insertChildren();
    QScriptValue insertChild();
    QScriptValue removeChildren();

    QScriptValue children() const;
    QScriptValue visibleChildren() const;

    QScriptValue parent() const;
    QScriptValue setParent();

protected:
    Drawable* m_drawable;
};

Q_DECLARE_METATYPE(QList<Wrapper_Drawable*>)
Q_DECLARE_METATYPE(Wrapper_Drawable*)

#endif // WRAPPER_DRAWABLE_H

CPP:

#include "wrapper_drawable.h"

Wrapper_Drawable::Wrapper_Drawable(Drawable *drawable, QObject *parent) :
    QObject(parent), m_drawable(drawable)
{
}

Drawable* Wrapper_Drawable::drawable() const {
    return m_drawable;
}

QScriptValue Wrapper_Drawable::removeChildren() {
    Wrapper_Drawable* wrapper_drawable = qscriptvalue_cast<Wrapper_Drawable*>(context()->thisObject());

    if(!wrapper_drawable)
        return context()->throwError(QScriptContext::TypeError, "Drawable.insertChildren: this object is not a Drawable");

    Drawable* drawable = wrapper_drawable->drawable();

    if(!drawable)
        return context()->throwError(QScriptContext::TypeError, "Drawable.insertChildren: no Drawable wrapped");

    if(argumentCount() != 1)
        return context()->throwError(QScriptContext::SyntaxError, "Drawable.insertChildren takes exactly 1 argument");

    // TODO: maybe allow multiple arguments

    if(!argument(0).isNumber())
        return context()->throwError(QScriptContext::TypeError, "Drawable.insertChildren: argument 0 should be a number");

    unsigned int position = argument(0).toNumber();

    if(!argument(1).isNumber())
        return context()->throwError(QScriptContext::TypeError, "Drawable.insertChildren: argument 1 should be a number");

    unsigned int count = argument(1).toNumber();

    return engine()->toScriptValue(drawable->removeChildren(position, count));
}

QScriptValue Wrapper_Drawable::visibleChildren() const {
    Wrapper_Drawable* wrapper_drawable = qscriptvalue_cast<Wrapper_Drawable*>(context()->thisObject());

    if(!wrapper_drawable)
        return context()->throwError(QScriptContext::TypeError, "Drawable.children: this object is not a Drawable");

    Drawable* drawable = wrapper_drawable->drawable();

    if(!drawable)
        return context()->throwError(QScriptContext::TypeError, "Drawable.children: no Drawable wrapped");

    if(argumentCount() > 0)
        return context()->throwError(QScriptContext::SyntaxError, "Drawable.children does not take any argument");

    QList<Drawable*> drawables = drawable->visibleChildren();
    QList<Wrapper_Drawable*> wrapper_drawables;
    for(QList<Drawable*>::const_iterator it = drawables.constBegin(), end = drawables.constEnd();
        it != end; ++it)
    {
        wrapper_drawables.append(new Wrapper_Drawable(*it));
    }

    return engine()->toScriptValue(wrapper_drawables);
}

MeshDrawable:

#ifndef WRAPPER_MESHDRAWABLE_H
#define WRAPPER_MESHDRAWABLE_H

#include "wrapper_drawable.h"

#include "../meshdrawable.h"

class Wrapper_MeshDrawable : public Wrapper_Drawable
{
    Q_OBJECT
public:
    Wrapper_MeshDrawable(MeshDrawable* meshDrawable = 0, QObject *parent = 0);

    virtual MeshDrawable* drawable() const;

public slots:
    QScriptValue addTri();
    QScriptValue addQuad();
    QScriptValue setSmoothing();
};

Q_DECLARE_METATYPE(Wrapper_MeshDrawable*)

#endif // WRAPPER_MESHDRAWABLE_H

CPP:

#include "wrapper_meshdrawable.h"

Wrapper_MeshDrawable::Wrapper_MeshDrawable(MeshDrawable *meshDrawable, QObject *parent) :
    Wrapper_Drawable(meshDrawable, parent)
{
}

MeshDrawable* Wrapper_MeshDrawable::drawable() const {
    return static_cast<MeshDrawable*>(Wrapper_Drawable::drawable());
}

QScriptValue Wrapper_MeshDrawable::addTri() {

}

QScriptValue Wrapper_MeshDrawable::addQuad() {

}

QScriptValue Wrapper_MeshDrawable::setSmoothing() {

}

And finally ScriptManager (where I declare the various types to the script engine):

#ifndef SCRIPTMANAGER_H
#define SCRIPTMANAGER_H

#include <QtScript/QScriptEngine>
#include <QtScriptTools/QScriptEngineDebugger>
#include <QtScriptTools/QtScriptTools>
#include <QStringList>
#include <QObject>

#include "utility.h"

class ScriptManager : public QObject {
    Q_OBJECT
public:
    ScriptManager();

public slots:
    QString interprete(QString command);

private:
    void initializeFunctions();

    QScriptEngine* m_scriptEngine;

    QScriptEngineDebugger* m_scriptEngineDebugger;
};

#endif // SCRIPTMANAGER_H

CPP

#include "scriptmanager.h"
#include "scenegraph.h"

#include "meshdrawable.h"
#include "objdrawable.h"

#include <QScriptValueIterator>

#include "wrappers/wrapper_camera.h"
#include "wrappers/wrapper_cameramanager.h"
#include "wrappers/wrapper_drawable.h"
#include "wrappers/wrapper_meshdrawable.h"
#include "wrappers/wrapper_drawablemanager.h"
#include "wrappers/wrapper_scenegraph.h"
#include "wrappers/wrapper_shadermanager.h"

QString ScriptManager::returnString = QString();

ScriptManager::ScriptManager() : m_scriptEngine(new QScriptEngine())
{
    initializeFunctions();
}

void ScriptManager::initializeFunctions() {
    qScriptRegisterQObjectMetaType<QGLShaderProgram*>(m_scriptEngine);

    qScriptRegisterSequenceMetaType<QList<Wrapper_Drawable*> >(m_scriptEngine);

    QScriptValue function_ls = m_scriptEngine->newFunction(scriptFunction_ls);
    m_scriptEngine->globalObject().setProperty("ls", function_ls);

    QScriptValue function_print = m_scriptEngine->newFunction(scriptFunction_print);
    m_scriptEngine->globalObject().setProperty("print", function_print);

//    m_scriptEngine->setDefaultPrototype(qMetaTypeId<Observer*>(),
//                                        Observer::getDefaultPrototype(m_scriptEngine));

    Wrapper_Drawable* wrapper_drawable = new Wrapper_Drawable();
    QScriptValue wrapper_drawable_obj = m_scriptEngine->newQObject(wrapper_drawable);
    m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_Drawable*>(),
                                        wrapper_drawable_obj);
    m_scriptEngine->globalObject().setProperty("Drawable", wrapper_drawable_obj);

    Wrapper_MeshDrawable* wrapper_meshDrawable = new Wrapper_MeshDrawable();
    QScriptValue wrapper_meshDrawable_obj = m_scriptEngine->newQObject(wrapper_meshDrawable);
    m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_MeshDrawable*>(),
                                        wrapper_meshDrawable_obj);
    m_scriptEngine->globalObject().setProperty("MeshDrawable", wrapper_meshDrawable_obj);

    Wrapper_Camera* wrapper_camera = new Wrapper_Camera();
    m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_Camera*>(),
                                        m_scriptEngine->newQObject(wrapper_camera));

    Wrapper_CameraManager* wrapper_cameraManager = new Wrapper_CameraManager();
    m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_CameraManager*>(),
                                        m_scriptEngine->newQObject(wrapper_cameraManager));

    Wrapper_DrawableManager* wrapper_drawableManager = new Wrapper_DrawableManager();
    m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_DrawableManager*>(),
                                        m_scriptEngine->newQObject(wrapper_drawableManager));

    Wrapper_SceneGraph* wrapper_sceneGraph = new Wrapper_SceneGraph(SceneGraph::instance());
    m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_SceneGraph*>(),
                                        m_scriptEngine->newQObject(wrapper_sceneGraph));

    QScriptValue object_sceneGraph = m_scriptEngine->newQObject("sceneGraph", wrapper_sceneGraph);
    m_scriptEngine->globalObject().setProperty("sceneGraph", object_sceneGraph);

    Wrapper_ShaderManager* wrapper_shaderManager = new Wrapper_ShaderManager();
    m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_ShaderManager*>(),
                                        m_scriptEngine->newQObject(wrapper_shaderManager));

    m_scriptEngineDebugger = new QScriptEngineDebugger();
    m_scriptEngineDebugger->attachTo(m_scriptEngine);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情定在深秋 2025-01-01 14:03:54

您需要定义您的原型。这很快就会变得复杂,但是 http://doc.qt.io/archives/qt-4.7/scripting.html#making-use-of-prototype-based-inheritance 是一个很好的参考。

You need to define your prototypes. This gets complicated fairly quickly, but http://doc.qt.io/archives/qt-4.7/scripting.html#making-use-of-prototype-based-inheritance is a good reference for how.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文