QtScript how to handle custom classes derived from QObject
Hi,
I wrote two C++ classes in which I defined a point and a line that derive from qObject in order to make them scriptable using QtScript. I also wrote a simple form in which I would like to create istances of points and lines and use their methods to create more complex entities.
I declared these objects in this way:
Code:
Q_SCRIPT_DECLARE_QMETAOBJECT
(point,
QObject*);
Q_SCRIPT_DECLARE_QMETAOBJECT
(line,
QObject*);
Then I created a QScriptEngine in my form constructor and I linked the two classes to the engine in this way:
Code:
engine->globalObject().setProperty("point",engine->scriptValueFromQMetaObject<point>;());
engine->globalObject().setProperty("line",engine->scriptValueFromQMetaObject<line>;());
Now I can create for example a point in my form and use its methods without any problem:
Code:
P0=new point();
P0.getX();
The problems come when I want to use line's methods that give as a return type a point object:
--- line.h ---
Code:
{
Q_OBJECT
public:
//! Line object destructor.
~line();
Q_INVOKABLE point getLineVertex(float);
private:
point P0_;
point P1_;
float a_;
float b_;
float c_;
};
---line.cpp---
Code:
P0_.setX(0); P0_.setY(0); P0_.setZ(0);
P1_.setX(1.0); P1_.setY(1.0); P1_.setZ(1.0);
a_=P1_.getX()-P1_.getX(); //If using the diff operation
b_=P1_.getY()-P1_.getY(); //it will generate a new Point
c_=P1_.getZ()-P1_.getZ();
}
point line::getLineVertex(float t_){
point P(P0_.getX()+a_*t_, P0_.getY()+b_*t_, P0_.getZ()+c_*t_);
return P;
}
The program complains asking to register the 'point' type with qScriptRegisterMetaType() function. After googling for a day I really haven't find a solution to this issue (it seems that I have to define two functions to convert the class type to the QScriptValue type but these solutions work only on pointers and I prefer to avoid them)....
I'm kindly asking you if is there someone that can help me in some way :-)!
Thank you very much
Norman
Re: QtScript how to handle custom classes derived from QObject
I have solved it by myself :)
Thank you
Norman
Re: QtScript how to handle custom classes derived from QObject
Quote:
Originally Posted by
Norman
I have solved it by myself :)
Thank you
Norman
Even if your thread is very old, it is always helpful to publish your solution.