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*);
Q_SCRIPT_DECLARE_QMETAOBJECT(point, QObject*);
Q_SCRIPT_DECLARE_QMETAOBJECT(line, QObject*);
To copy to clipboard, switch view to plain text mode
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>;());
engine->globalObject().setProperty("point",engine->scriptValueFromQMetaObject<point>;());
engine->globalObject().setProperty("line",engine->scriptValueFromQMetaObject<line>;());
To copy to clipboard, switch view to plain text mode
Now I can create for example a point in my form and use its methods without any problem:
P0=new point();
P0.getX();
P0=new point();
P0.getX();
To copy to clipboard, switch view to plain text mode
The problems come when I want to use line's methods that give as a return type a point object:
--- line.h ---
{
Q_OBJECT
public:
//! Line object destructor.
~line();
Q_INVOKABLE point getLineVertex(float);
private:
point P0_;
point P1_;
float a_;
float b_;
float c_;
};
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_;
};
To copy to clipboard, switch view to plain text mode
---line.cpp---
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks