Trolltech Home | QSA-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

QSA-interest Archive, March 2006
Wish for QSA 2.0 (connecting object-signals with scriptfunctions)


Message 1 in thread

Hi trolls,

I would like to add a wish to the wish-list of QSA 2.0 :)

Adding new QObjects to the QInterpreter can be done through an 
QSObjectFactory. That way all slots of the QObjects (for example MyObj) are 
available in a Qt Script through
	var myObj = new MyObj();
	myObj.mySlot();

It is possible to connect a signal of myObj with a script-function like this:
	connect( myObj, "mySignal(bool)", this, "scriptFunction" );
This works as long as a script is running and is lost after the script stops 
even if myObj is still alive (by changing its parent).

To keep a connection alive independent of the state of a script the function 
QInterpreter::addTransientSignalHandler() is used like this:
	interpreter->addTransientSignalHandler( 
		myButton, 
		SIGNAL(clicked()), 
		"classA.obj.calculate");

My wish:
I would like to establish a connection of a QObject created in a script (like 
myObj) through a script-function. This connection shall stay alive as long as 
the QObject isn´t deleted. (this is not possible with simply using "connect" 
in a script).

--------------------------
I tought of adding the function through an QObject "System" with a slot
	Application.System.addTransientConnection(
		QObject* obj, 
		QString signal,
		QString scriptFunction)
which calls 
	interpreter->addTransientSignalHandler()
=>But what would the string "signal" has to look like???

Would that work?
What happens if a script is running through the same interpreter, which 
recives a signal because of addTransientSignalHandler()?
What happens if the object gets deleted, whichs signal got connected to the 
interpreter?

	Greetings
		Jens
	

To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx


Message 2 in thread

Jens,

I have explored the lower-level workings of signals and slots, and  
have discovered how to convert a string to a "proper" signal  
identifier, the way QSA does when you call connect() from a script.

Basically, it's like this:

QString("%1%2").arg(SIGNAL_CODE).arg(signal)

The macro SIGNAL_CODE is defined in the Qt headers, and provides the  
"magic" that Qt needs.  So you could definitely make your System  
object with an addTransientSignalHandler slot, using the above.

Oh, yeah, one other thing I forgot, since you cannot pass QSA  
function objects into C++ slots, you can work around it by writing  
some helper script code.  Prepend your script with a function called  
addTransientSignalHandler() which takes the object, signal, and  
script function, and then stores this connection in an array.  (Or,  
you might even be able to attach it to the sending object itself, as  
a script variable.)  Then, in C++ land, actually just make your  
transient-signal-handler go to another predefined script function,  
like _doSignal or whatever.  That function would take the object,  
signal name, and so on, and then locate the stored reference to the  
appropriate script function, and then run it.  It's perhaps a little  
bit cumbersome, but it works.  I've done something very similar in my  
code.

Joel Nordell
Software Engineer
ONEAC Corp.


On Mar 18, 2006, at 4:21 AM, Jens G. wrote:

> Hi trolls,
>
> I would like to add a wish to the wish-list of QSA 2.0 :)
>
> Adding new QObjects to the QInterpreter can be done through an
> QSObjectFactory. That way all slots of the QObjects (for example  
> MyObj) are
> available in a Qt Script through
> 	var myObj = new MyObj();
> 	myObj.mySlot();
>
> It is possible to connect a signal of myObj with a script-function  
> like this:
> 	connect( myObj, "mySignal(bool)", this, "scriptFunction" );
> This works as long as a script is running and is lost after the  
> script stops
> even if myObj is still alive (by changing its parent).
>
> To keep a connection alive independent of the state of a script the  
> function
> QInterpreter::addTransientSignalHandler() is used like this:
> 	interpreter->addTransientSignalHandler(
> 		myButton,
> 		SIGNAL(clicked()),
> 		"classA.obj.calculate");
>
> My wish:
> I would like to establish a connection of a QObject created in a  
> script (like
> myObj) through a script-function. This connection shall stay alive  
> as long as
> the QObject isn´t deleted. (this is not possible with simply using  
> "connect"
> in a script).
>
> --------------------------
> I tought of adding the function through an QObject "System" with a  
> slot
> 	Application.System.addTransientConnection(
> 		QObject* obj,
> 		QString signal,
> 		QString scriptFunction)
> which calls
> 	interpreter->addTransientSignalHandler()
> =>But what would the string "signal" has to look like???
>
> Would that work?
> What happens if a script is running through the same interpreter,  
> which
> recives a signal because of addTransientSignalHandler()?
> What happens if the object gets deleted, whichs signal got  
> connected to the
> interpreter?
>
> 	Greetings
> 		Jens
> 	
>
> To unsubscribe - send "unsubscribe" in the subject to qsa-interest- 
> request@xxxxxxxxxxxxx

To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx