Results 1 to 7 of 7

Thread: how to call user defined function of user defined call from QtScriptEngine evaluate

  1. #1

    Default how to call user defined function of user defined call from QtScriptEngine evaluate

    Hi,

    I am having a c++ class CRectangleItems. this class have lots of different functions. I want to call these function by using QtScript. Means i will create object of CRectangle and will define as newQObject with QtScriptEngine. I will provide separate script editor, from editor user will call and of function from CRectangleItems . and all that script will be given to QtScriptEngine.evaluate function. Please guide me to achieve this.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to call user defined function of user defined call from QtScriptEngine evalua

    It's enough if your CRectangleItems class declares all methods you want to call on it as slots. Then you can call them from within QtScript (provided the instance of the class is exported to the scripting environment).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3

    Default Re: how to call user defined function of user defined call from QtScriptEngine evalua

    yes i tried to do it in same way, but its not working. have a look towards the code

    QScriptEngine scriptEngine;
    CRectangleItem *rectangle = new CRectangleItem();
    QScriptValue scriptobj = scriptEngine.newQObject(rectangle,QScriptEngine::S criptOwnership);
    scriptEngine.globalObject().setProperty("rectangle ",scriptobj);
    QScriptValue result = scriptEngine.evaluate(scriptstring);



    In CRectangleItem class i have declared a slot drawr()

    then can i call it from script editor like

    rectangle.drawr(); ----- I will give this string to evaluate function.... is it correct way of calling, please let me know

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to call user defined function of user defined call from QtScriptEngine evalua

    Quote Originally Posted by sheetalw View Post
    yes i tried to do it in same way, but its not working. have a look towards the code

    QScriptEngine scriptEngine;
    CRectangleItem *rectangle = new CRectangleItem();
    QScriptValue scriptobj = scriptEngine.newQObject(rectangle,QScriptEngine::S criptOwnership);
    scriptEngine.globalObject().setProperty("rectangle ",scriptobj);
    QScriptValue result = scriptEngine.evaluate(scriptstring);



    In CRectangleItem class i have declared a slot drawr()

    then can i call it from script editor like

    rectangle.drawr();
    And what happens when you do that?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5

    Default Re: how to call user defined function of user defined call from QtScriptEngine evalua

    string variable result shows a message " rectangle.drawr[undefined] is not a function"

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to call user defined function of user defined call from QtScriptEngine evalua

    This works fine for me:

    Qt Code:
    1. #include <QApplication>
    2. #include <QScriptEngine>
    3. #include <QtDebug>
    4.  
    5. class RItem : public QObject {
    6. Q_OBJECT
    7. public:
    8. RItem() : QObject() {}
    9. public slots:
    10. void drawr() { qDebug() << Q_FUNC_INFO; }
    11. };
    12.  
    13. #include "main.moc"
    14.  
    15. int main(int argc, char **argv) {
    16. QApplication app(argc, argv);
    17. QScriptEngine engine;
    18. RItem item;
    19. QScriptValue val = engine.newQObject(&item);
    20. engine.globalObject().setProperty("rectangle", val);
    21. engine.evaluate("rectangle.drawr()") ;
    22. return 0;
    23. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7

    Default Re: how to call user defined function of user defined call from QtScriptEngine evalua

    Thanks a lot. I declared slot as private that's why it was not working for me.

Similar Threads

  1. How to get user-defined message?
    By ponponfish in forum Qt Programming
    Replies: 3
    Last Post: 17th May 2011, 17:19
  2. QSqlQueryModel + user defined roles
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 7th December 2009, 08:29
  3. Resizing user defined QGraphicsItem
    By manti_madhu in forum Qt Programming
    Replies: 0
    Last Post: 9th September 2009, 10:35
  4. Using User Defined Libraries
    By csvivek in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2008, 20:47
  5. User Defined Signal?
    By rajeshs in forum Newbie
    Replies: 2
    Last Post: 18th December 2007, 12:42

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.