Results 1 to 5 of 5

Thread: Call object method using qtscript

  1. #1
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Call object method using qtscript

    Hi,

    I want to use QtScript in one of my application. for this I have created following class.


    datefunction.h

    Qt Code:
    1. #ifndef DATEFUNCTION_H
    2. #define DATEFUNCTION_H
    3.  
    4. #include <QtCore>
    5.  
    6. class DateFunction : public QObject
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. explicit DateFunction(QObject *parent = 0);
    12.  
    13. signals:
    14.  
    15. public slots:
    16. int daysInYear(QString year);
    17. };
    18.  
    19. #endif // DATEFUNCTION_H
    To copy to clipboard, switch view to plain text mode 


    datefunction.cpp

    Qt Code:
    1. #include "datefunction.h"
    2.  
    3. DateFunction::DateFunction(QObject *parent) :
    4. QObject(parent)
    5. {
    6. }
    7.  
    8. int DateFunction::daysInYear(QString year)
    9. {
    10. int str=0;
    11. QDate d1 = QDate::fromString(year,"yyyy");
    12. if (!d1.isValid())
    13. return str;
    14. str = d1.daysInYear();
    15. return str;
    16. }
    To copy to clipboard, switch view to plain text mode 

    Now I want user to use this in qtscript and user should be able to call daysInYear() method in QScriptEngine.evaluate() function by passing this method and required parameter as a string.
    Can anybody help me on this.


    Regards

    Manish

  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: Call object method using qtscript

    What have you tried so far?
    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
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Call object method using qtscript

    I have come up upto here only

    Qt Code:
    1. #include <QtCore>
    2. #include "datefunction.h"
    3. #include <QtScript>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8. QScriptEngine eng;
    9. QObject *obj = new DateFunction;
    10. QScriptValue val = eng.newQObject(obj,QScriptEngine::ScriptOwnership);
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Not sure how to use val variable to get class function call


    Manish

  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: Call object method using qtscript

    Fetch the global object and set val as a property of that object. Then you can use this property to access your function.
    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
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Call object method using qtscript

    Additionally, have a look at registering a stand-alone function
    http://qt-project.org/doc/qt-4.8/qsc...ml#newFunction

    Cheers,
    _

Similar Threads

  1. Call method of Object in different Thread
    By Qiieha in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2012, 13:14
  2. How to call a function in the mother object from a child object?
    By Momergil in forum General Programming
    Replies: 4
    Last Post: 18th December 2011, 15:49
  3. C++ method call
    By ^NyAw^ in forum General Programming
    Replies: 4
    Last Post: 19th November 2011, 12:19
  4. QtScript. How to add created object to script?
    By sergey_85 in forum Qt Programming
    Replies: 1
    Last Post: 18th February 2010, 14:24
  5. Replies: 3
    Last Post: 16th May 2007, 11:07

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.