Hello.
How to register pointer type in qScriptRegisterMetatype?
h-file.
namespace PFT3 {
class TaskEngine
: public QObject {Q_OBJECT
public:
static const char * TaskEngine::nameInScript;
static const char * TaskEngine::scriptTasksNamespaceName;
void createInQScriptEngine(QScriptEngine &scriptEngine);
};
namespace PFT3_Tasks {
class LIBPFT3SHARED_EXPORT BaseData
: public QObject { Q_OBJECT
};
class BaseDataScriptProto
: public QObject,
protected QScriptable
{ Q_OBJECT
};
}// end of namespace PFT3_Tasks
}//end of PFT3 namespace
Q_DECLARE_METATYPE(PFT3::PFT3_Tasks::BaseData)
Q_DECLARE_METATYPE(PFT3::PFT3_Tasks::BaseData*)
Q_DECLARE_METATYPE(const PFT3::PFT3_Tasks::BaseData*)
namespace PFT3 {
class TaskEngine : public QObject {
Q_OBJECT
public:
static const char * TaskEngine::nameInScript;
static const char * TaskEngine::scriptTasksNamespaceName;
void createInQScriptEngine(QScriptEngine &scriptEngine);
};
namespace PFT3_Tasks {
class LIBPFT3SHARED_EXPORT BaseData : public QObject {
Q_OBJECT
};
class BaseDataScriptProto : public QObject, protected QScriptable {
Q_OBJECT
};
}// end of namespace PFT3_Tasks
}//end of PFT3 namespace
Q_DECLARE_METATYPE(PFT3::PFT3_Tasks::BaseData)
Q_DECLARE_METATYPE(PFT3::PFT3_Tasks::BaseData*)
Q_DECLARE_METATYPE(const PFT3::PFT3_Tasks::BaseData*)
To copy to clipboard, switch view to plain text mode
cpp-file
QScriptValue PFT3TaskBaseDataToScriptValue(QScriptEngine *engine, PFT3::PFT3_Tasks::BaseData * const &in){
return engine->newQObject(in, QScriptEngine::AutoOwnership);
}
void PFT3TaskBaseDataFromScriptValue(const QScriptValue &object, PFT3::PFT3_Tasks::BaseData * &out){
out = qobject_cast<PFT3::PFT3_Tasks::BaseData*>(object.toQObject());
}
namespace PFT3 {
const char * TaskEngine::nameInScript = "pft3TaskEngine";
const char * TaskEngine::scriptTasksNamespaceName = "PFT3_Tasks";//Changing this name doesn't changes 'PFT3_Tasks::BaseData*' string in error. This is only coincidence.
void TaskEngine::createInQScriptEngine(QScriptEngine &scriptEngine){
/*Create single for given scriptEngine TaskEngine*/
QScriptValue scriptTaskEngineObject = scriptEngine.newQObject(this);
scriptEngine.globalObject().setProperty(nameInScript, scriptTaskEngineObject);
/*Adding prototypes*/
QScriptValue scriptTasksNamespace
= scriptEngine.
newQObject(new QObject(), QScriptEngine
::ScriptOwnership);
scriptEngine.globalObject().setProperty(scriptTasksNamespaceName, scriptTasksNamespace);
/*BaseData*/
PFT3_Tasks::BaseDataScriptProto *baseDataPrototypeObject = new PFT3_Tasks::BaseDataScriptProto (&scriptEngine);
const QScriptValue baseDataProto = scriptEngine.newQObject(baseDataPrototypeObject, QScriptEngine::ScriptOwnership);
scriptEngine.setDefaultPrototype(qMetaTypeId<PFT3::PFT3_Tasks::BaseData>(), baseDataProto);
QScriptValue baseDataCtor = scriptEngine.newFunction(&PFT3_Tasks::BaseData::construct, baseDataProto);
scriptEngine.globalObject().property(scriptTasksNamespaceName).setProperty("BaseData", baseDataCtor);
/*-------------------------------------------------------------------------------------------------------*/
qScriptRegisterMetaType(&scriptEngine, PFT3TaskBaseDataToScriptValue, PFT3TaskBaseDataFromScriptValue);//I register it here.
/*-------------------------------------------------------------------------------------------------------*/
}
}// end of namespace PFT3
QScriptValue PFT3TaskBaseDataToScriptValue(QScriptEngine *engine, PFT3::PFT3_Tasks::BaseData * const &in){
return engine->newQObject(in, QScriptEngine::AutoOwnership);
}
void PFT3TaskBaseDataFromScriptValue(const QScriptValue &object, PFT3::PFT3_Tasks::BaseData * &out){
out = qobject_cast<PFT3::PFT3_Tasks::BaseData*>(object.toQObject());
}
namespace PFT3 {
const char * TaskEngine::nameInScript = "pft3TaskEngine";
const char * TaskEngine::scriptTasksNamespaceName = "PFT3_Tasks";//Changing this name doesn't changes 'PFT3_Tasks::BaseData*' string in error. This is only coincidence.
void TaskEngine::createInQScriptEngine(QScriptEngine &scriptEngine){
/*Create single for given scriptEngine TaskEngine*/
QScriptValue scriptTaskEngineObject = scriptEngine.newQObject(this);
scriptEngine.globalObject().setProperty(nameInScript, scriptTaskEngineObject);
/*Adding prototypes*/
QScriptValue scriptTasksNamespace = scriptEngine.newQObject(new QObject(), QScriptEngine::ScriptOwnership);
scriptEngine.globalObject().setProperty(scriptTasksNamespaceName, scriptTasksNamespace);
/*BaseData*/
PFT3_Tasks::BaseDataScriptProto *baseDataPrototypeObject = new PFT3_Tasks::BaseDataScriptProto (&scriptEngine);
const QScriptValue baseDataProto = scriptEngine.newQObject(baseDataPrototypeObject, QScriptEngine::ScriptOwnership);
scriptEngine.setDefaultPrototype(qMetaTypeId<PFT3::PFT3_Tasks::BaseData>(), baseDataProto);
QScriptValue baseDataCtor = scriptEngine.newFunction(&PFT3_Tasks::BaseData::construct, baseDataProto);
scriptEngine.globalObject().property(scriptTasksNamespaceName).setProperty("BaseData", baseDataCtor);
/*-------------------------------------------------------------------------------------------------------*/
qScriptRegisterMetaType(&scriptEngine, PFT3TaskBaseDataToScriptValue, PFT3TaskBaseDataFromScriptValue);//I register it here.
/*-------------------------------------------------------------------------------------------------------*/
}
}// end of namespace PFT3
To copy to clipboard, switch view to plain text mode
Then I write on QtScript:
//delete task;
var task = new PFT3_Tasks.BaseData(1, 2, 6000, 2000, true);
pft3TaskEngine.appendTask(task);
//delete task;
var task = new PFT3_Tasks.BaseData(1, 2, 6000, 2000, true);
pft3TaskEngine.appendTask(task);
To copy to clipboard, switch view to plain text mode
And get script exception,that I parse to output:
Error in script caused the exception:
"TypeError: cannot call appendTask(): argument 1 has unknown type `PFT3_Tasks::BaseData*' (register the type with qScriptRegisterMetaType())"
at executing line 3.
Call stack:
1. <anonymous>()@Script, entered by user:3
http://lists.qt.nokia.com/public/qt-...ry/002051.html I read and tried with typedef.
Making Applications Scriptable I read too and many times.
How to do this correctly?
Thank you for your replies.
Bookmarks