PDA

View Full Version : QtScript how to handle custom classes derived from QObject



Norman
28th July 2009, 10:03
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:


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:



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:



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 ---

class line : public QObject
{
Q_OBJECT
public:
line(QObject* parent=0);
//! Line object destructor.
~line();
Q_INVOKABLE point getLineVertex(float);

private:
point P0_;
point P1_;
float a_;
float b_;
float c_;
};


---line.cpp---

line::line(QObject * parent):QObject(parent){
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

Norman
30th July 2009, 18:07
I have solved it by myself :)

Thank you

Norman

qwert
18th June 2013, 10:31
I have solved it by myself :)

Thank you

Norman

Even if your thread is very old, it is always helpful to publish your solution.