PDA

View Full Version : How to call any event handler written in script from c++ class



sheetalw
24th December 2012, 09:20
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.

lanz
24th December 2012, 10:20
You can connect ui signal to function handler in your script like here:
http://doc.qt.digia.com/qt/scripting.html#signal-to-function-connections

Or you can call function from scriptvalue, see here:
http://doc.qt.digia.com/qt/scripting.html#calling-a-qt-script-function-from-c

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

sheetalw
24th December 2012, 12:24
Thanks for reply , i have gone through that links already, i did not understood it. do u have any example regarding this?

lanz
24th December 2012, 12:55
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):


// create new script wrapper around ui button
QScriptValue button = script_engine.newQObject (ui.myButton);
// get reference to script engine's global object
QScriptValue &global_object = script_engine.globalObject ();
// set property "myButton" to the newly created wrapper
global_object.setProperty ("myButton", button);

Then in script you can connect to the slots and signals:


function myButtonClickHandler () {
....
};

myButton.clicked.connect (myButtonClickHandler);

sheetalw
26th December 2012, 09:07
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.

wysota
26th December 2012, 11:49
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.

lanz
27th December 2012, 05:51
Or if you don't want to write C++ handler for event, you may use qScriptConnect ()
From the docs:


QLineEdit *edit1 = new QLineEdit(...);
QLineEdit *edit2 = new QLineEdit(...);

QScriptValue handler = eng.evaluate("(function() { print('I am', this.name); })");
QScriptValue obj1 = eng.newObject();
obj1.setProperty("name", "the walrus");
QScriptValue obj2 = eng.newObject();
obj2.setProperty("name", "Sam");

qScriptConnect(edit1, SIGNAL(returnPressed()), obj1, handler);
qScriptConnect(edit2, SIGNAL(returnPressed()), obj2, handler);

I think you can specify global object instead of obj1/obj2 to use global functions.

sheetalw
31st December 2012, 09:33
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?

wysota
2nd January 2013, 13:52
Can you prepare a minimal compilable example reproducing the problem?

sheetalw
7th January 2013, 06:34
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...

wysota
7th January 2013, 11:15
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.