tags:

views:

56

answers:

1

Hello,
I have written a custom class which is available in QtScript through a Prototype. Also another global class is available which should be used to print the custom class generated in QtScript.

This is my custom class (very simple ;) ):

class Message  
{  
public:  
  int source;  
  int target;  
};  

This is the prototype I am using:

class MessagePrototype : public QObject, public QScriptable
{
  Q_OBJECT
  Q_PROPERTY(int source READ getSource WRITE setSource)
  Q_PROPERTY(int target READ getTarget WRITE setTarget)
public:
  void setSource(const int source);
  int getSource() const;
  void setTarget(const int target);
  int getTarget() const;
};

The setter / getter are only changing / printing the corresponding Message object through a qscriptvalue_cast(QScriptable::thisObject());

Now my script looks like this:

var test = new Message;
test.source = 5;
print(test.source);
GlobalObject.sendMessage(test);

So, the script compiles fine and the print() command does what it should, it prints 5. But the problem is the sendMessage function of my GlobalObject:

void MessageAnalysis::sendMessage(Message msg)
{
  qDebug() << "[Message]" << msg.source << msg.target;
}

This piece of code always prints: "[Message] 0 0".

MessageAnalysis is registered as "GlobalObject" for QtScript. Also I have registered Message and Message* as Metatypes and the constructor, prototype and everything else. This seems to work.

Does anyone knows why the values have been changed in QtScript but are not accessible from my C++ function? Or what I am doing wrong?

A: 

Ok. After several attempts I fixed it.
I changed the sendMessage function to accept a QScriptValue instead of a Message as parameter. Now I can get the properties out of it without problems. Seems to work fine now :)

Tobias