Results 1 to 11 of 11

Thread: How to call any event handler written in script from c++ class

  1. #1

    Default How to call any event handler written in script from c++ class

    I am having a event handler like mouseLButtonDown() written in script. This event handler need to be called after click on ui of application. How can i call this event handler from my c++ application class. Please let me know if anyone have any idea.

  2. #2
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to call any event handler written in script from c++ class

    You can connect ui signal to function handler in your script like here:
    http://doc.qt.digia.com/qt/scripting...on-connections

    Or you can call function from scriptvalue, see here:
    http://doc.qt.digia.com/qt/scripting...unction-from-c

    Assuming you are using qt script in the first place .)

  3. #3

    Default Re: How to call any event handler written in script from c++ class

    Thanks for reply , i have gone through that links already, i did not understood it. do u have any example regarding this?

  4. #4
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to call any event handler written in script from c++ class

    Ok, let's assume you have button myButton in the ui.
    First, you should register your button in the script engine you using by placing it into
    property of a global object (or any other object):
    Qt Code:
    1. // create new script wrapper around ui button
    2. QScriptValue button = script_engine.newQObject (ui.myButton);
    3. // get reference to script engine's global object
    4. QScriptValue &global_object = script_engine.globalObject ();
    5. // set property "myButton" to the newly created wrapper
    6. global_object.setProperty ("myButton", button);
    To copy to clipboard, switch view to plain text mode 
    Then in script you can connect to the slots and signals:
    Qt Code:
    1. function myButtonClickHandler () {
    2. ....
    3. };
    4.  
    5. myButton.clicked.connect (myButtonClickHandler);
    To copy to clipboard, switch view to plain text mode 

  5. #5

    Default Re: How to call any event handler written in script from c++ class

    do I need to write ----.----.connect() statement in script?.......cant i manage all these stuff from c++ code, n just keep event handler in script? when event occur instead of handler from system, this handler from script is called.

  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 any event handler written in script from c++ class

    You don't have to do it from within QtScript. You can extract any script function from the scripting environment and store it within a QScriptValue object. Later you can call QScriptValue::call() on it any time you want.
    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
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to call any event handler written in script from c++ class

    Or if you don't want to write C++ handler for event, you may use qScriptConnect ()
    From the docs:
    Qt Code:
    1. QLineEdit *edit1 = new QLineEdit(...);
    2. QLineEdit *edit2 = new QLineEdit(...);
    3.  
    4. QScriptValue handler = eng.evaluate("(function() { print('I am', this.name); })");
    5. QScriptValue obj1 = eng.newObject();
    6. obj1.setProperty("name", "the walrus");
    7. QScriptValue obj2 = eng.newObject();
    8. obj2.setProperty("name", "Sam");
    9.  
    10. qScriptConnect(edit1, SIGNAL(returnPressed()), obj1, handler);
    11. qScriptConnect(edit2, SIGNAL(returnPressed()), obj2, handler);
    To copy to clipboard, switch view to plain text mode 
    I think you can specify global object instead of obj1/obj2 to use global functions.

  8. #8

    Default Re: How to call any event handler written in script from c++ class

    Hi,
    I tried a lot to do in above mentioned method. qScriptConnect is getting called and returns true which means connection is established successfully and function/handler is called. but operations specified in handler are not getting performed. Handler is not working...

    If a call the same handler as a function it works, but if i try to work with it as a slot it is not working,.....can u suggest anything?

  9. #9
    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 any event handler written in script from c++ class

    Can you prepare a minimal compilable example reproducing the problem?
    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.


  10. #10

    Default Re: How to call any event handler written in script from c++ class

    here it is ...

    QScriptValue obj = m_scriptEngine.globalObject();
    obj.setProperty("OnLBtnDwn()",QScriptValue());

    QScriptValue slot = m_scriptEngine.evaluate(scriptstring); // scriptstring will contain actual handler/ function OnLBtnDwn(){. this function also calls //many slots to perform operations..} from script.

    QScriptValue result = qScriptConnect(ui.m_btnRunScript,SIGNAL(released() ),obj,slot);

    as it is not working please let me know what i m doing wrong...

  11. #11
    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 any event handler written in script from c++ class

    This is not a compilable example. This is four lines of code hanging in thin air.

    By the way, the second line of your snippet doesn't make much sense. And please use [code] tags.
    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.


Similar Threads

  1. QMake Call a pre-written makefile from a SUBDIRS project
    By Mookie in forum Qt Tools
    Replies: 0
    Last Post: 12th December 2011, 14:05
  2. Replies: 0
    Last Post: 23rd November 2010, 20:54
  3. qt script signal handler
    By wookoon in forum Newbie
    Replies: 0
    Last Post: 5th July 2010, 12:17
  4. Qt script signal handler
    By asvil in forum Qt-based Software
    Replies: 0
    Last Post: 17th April 2010, 11:42
  5. call TCL script frm QT4.5
    By BalaQT in forum Qt Programming
    Replies: 3
    Last Post: 30th October 2009, 13:17

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.