2

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.