我有一个Qt项目,该项目使用QtScript模块使我的应用程序的某些组件可编写脚本。
经过几次尝试使现有的类直接在QtScript中使用后,我选择使用继承QObject和QScriptable的包装类(主要是因为我有一些非qobject派生类,这些类是由继承QObject的其他人继承的因此,我不可能统一对待我的所有课程。
我现在正在尝试使用基于原型的继承。
我必须使用具有相应包装Drawable
和MeshDrawable
的类Wrapper_Drawable
和Wrapper_MeshDrawable
。 MeshDrawable
继承Drawable
,Wrapper_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
s。
由于我希望用户“相信”他使用的是Drawable
而不是Wrapper_Drawable
,我已声明:
m_scriptEngine->globalObject().setProperty("Drawable", wrapper_drawable_obj);
上面声明了wrapper_drawable_obj。
我想测试引擎是否识别Wrapper_MeshDrawable
s,即使它们是在Wrapper_Drawable
s的数组中声明的。
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有关,但我无法弄清楚究竟是什么。
提前感谢您的帮助。
编辑&gt; 我添加了ScriptManager的代码(处理脚本引擎并声明了各种类型),Wrapper_Drawable(只有一些方法,否则我无法发布此消息)和Wrapper_MeshDrawable。 / p>
我还仔细检查了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);
}
答案 0 :(得分:1)
您需要定义原型。这很复杂,但是http://doc.qt.io/archives/qt-4.7/scripting.html#making-use-of-prototype-based-inheritance是一个很好的参考。