Results 1 to 8 of 8

Thread: QTScript - getting the name of a QScriptValue

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: QTScript - getting the name of a QScriptValue

    Hi there!

    What functions are native and which are scripted in your design? And is that choice right?

    Usually you would implement the getname as a public slot (or better a q_property name) of your simulation-class which returns a QString. (The conversion between QString and QScriptValue is done automatically behind the scenes, when you call the function.).

    Your class needs to be derived from QObject, needs the Q_OBJECT macro and you need to register the classtype with qScriptRegisterMetaType implementing the toScriptValue and FromScriptValue Functions.

    Your native getname implementation in the SimulationClass can directly access the objects properties as usual..

    Qt Code:
    1. class Simulation : public QObject
    2. { Q_OBJECT
    3. Q_PROPERTY(QString name READ GetName)
    4. public slots:
    5. QString toString() {return QString("SimObject: %1").arg(GetName());}
    6. public:
    7. Simulation(QString pname) {_name = pname;}
    8. QString GetName() {return _name;}
    9. private:
    10. QString _name;
    11. };
    12. Q_DECLARE_METATYPE(Simulation*)
    13.  
    14. QScriptValue SimulationToScriptValue(QScriptEngine *engine, Simulation* const &in)
    15. {
    16. return engine->newQObject(in,QScriptEngine::QtOwnership);
    17. }
    18. void SimulationFromScriptValue(const QScriptValue &object, Simulation* &out)
    19. {
    20. out = qobject_cast<Simulation*>(object.toQObject());
    21. }
    22.  
    23. qScriptRegisterMetaType<Simulation*>(scripting, SimulationToScriptValue,SimulationFromScriptValue);
    To copy to clipboard, switch view to plain text mode 

    Your native getsimulation function (a function object of your global scripting object?) returns a pointer to an instance of your SimulationClass. Right?

    If however you are trying to implement something which returns a QScriptValue or needs QScriptValueLists as parameters, let me know. But I doubt, that you need it. If you do, the thing you are probably looking for is the "this" object of the script context.

  2. The following user says thank you to JohannesMunk for this useful post:

    android_ (28th October 2009)

Similar Threads

  1. QtScript: default constructor question
    By QPlace in forum Qt Programming
    Replies: 1
    Last Post: 22nd October 2009, 20:36
  2. QtScript evaluation question
    By QPlace in forum Qt Programming
    Replies: 0
    Last Post: 22nd October 2009, 04:46
  3. QScriptValue with simple typedef types
    By JohannesMunk in forum Newbie
    Replies: 9
    Last Post: 14th May 2009, 16:07
  4. QtScript Binding problem with QWidget
    By zack in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2009, 10:38
  5. QtScript Q_ENUM problem
    By oc2k1 in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2007, 17:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.