Hi there!
What functions are native and which are scripted in your design? And is that choice right?
Usually you would implement the getname as a public slot (or better a q_property name) of your simulation-class which returns a QString. (The conversion between QString and QScriptValue is done automatically behind the scenes, when you call the function.).
Your class needs to be derived from QObject, needs the Q_OBJECT macro and you need to register the classtype with qScriptRegisterMetaType implementing the toScriptValue and FromScriptValue Functions.
Your native getname implementation in the SimulationClass can directly access the objects properties as usual..
{ Q_OBJECT
Q_PROPERTY(QString name READ GetName
) public slots:
public:
Simulation
(QString pname
) {_name
= pname;
} private:
};
Q_DECLARE_METATYPE(Simulation*)
QScriptValue SimulationToScriptValue(QScriptEngine *engine, Simulation* const &in)
{
return engine->newQObject(in,QScriptEngine::QtOwnership);
}
void SimulationFromScriptValue(const QScriptValue &object, Simulation* &out)
{
out = qobject_cast<Simulation*>(object.toQObject());
}
qScriptRegisterMetaType<Simulation*>(scripting, SimulationToScriptValue,SimulationFromScriptValue);
class Simulation : public QObject
{ Q_OBJECT
Q_PROPERTY(QString name READ GetName)
public slots:
QString toString() {return QString("SimObject: %1").arg(GetName());}
public:
Simulation(QString pname) {_name = pname;}
QString GetName() {return _name;}
private:
QString _name;
};
Q_DECLARE_METATYPE(Simulation*)
QScriptValue SimulationToScriptValue(QScriptEngine *engine, Simulation* const &in)
{
return engine->newQObject(in,QScriptEngine::QtOwnership);
}
void SimulationFromScriptValue(const QScriptValue &object, Simulation* &out)
{
out = qobject_cast<Simulation*>(object.toQObject());
}
qScriptRegisterMetaType<Simulation*>(scripting, SimulationToScriptValue,SimulationFromScriptValue);
To copy to clipboard, switch view to plain text mode
Your native getsimulation function (a function object of your global scripting object?) returns a pointer to an instance of your SimulationClass. Right?
If however you are trying to implement something which returns a QScriptValue or needs QScriptValueLists as parameters, let me know. But I doubt, that you need it. If you do, the thing you are probably looking for is the "this" object of the script context.
Bookmarks