Dissecting the SS

J Duncan me at nospam.com
Thu Sep 28 18:37:37 PDT 2006


I also forgot to mention that slots need to disconnect from the signals 
when an object is deleted


J Duncan wrote:
> 
> 
> Bradley Smith wrote:
> 
>> J Duncan wrote:
>>
>>> // what QT-style SS would look like in D
>>>
>>> class AClass
>>> {
>>>     void aSignal(int i);
>>> }
>>>
>>> class BClass
>>> {
>>>     void aSlot(int i) { writefln("signal triggered %s", i); }
>>> }
>>>
>>> void main()
>>> {
>>>     AClass a = new AClass;
>>>
>>>     BClass b = new BClass;
>>>
>>>     connect( a, "aSignal(int)", b, "aSlot(int)" );
>>>
>>>     a.aSignal(10);    // -> writes "signal triggered 10"
>>> }
>>>
>>>
>>> i just wanted to give an example of how qt does it. the qt moc 
>>> generates the code for the function 'aSignal', this gives it a 
>>> feeling like its part of the language. the moc also generates code to 
>>> map signal and slot names to addresses. they also provide basic named 
>>> properties (named data members with set/get methods) this way. this 
>>> results in a pretty nice system that allows things like automatic 
>>> signal slot connections in gui classes based on names, (ex. the 
>>> 'edit1' widget automatically sends a 'textChanged' signal to a slot 
>>> on a parent widget called 'edit1_textChanged') i dont really have a 
>>> point to make, i just wanted to give a simple example of qt.
>>>
>>
>> Is there something more to the QT mechanism? I'm not a QT user, but I 
>> figure there must be more too it. Otherwise, why not use the following?
>>
>> import std.stdio;
>>
>> class AClass
>> {
>> //  void aSignal(int i);
>>     void delegate(int) aSignal;
>> }
>>
>> class BClass
>> {
>>     void aSlot(int i) { writefln("signal triggered %s", i); }
>> }
>>
>> void main()
>> {
>>     AClass a = new AClass;
>>
>>     BClass b = new BClass;
>>
>>     //connect( a, "aSignal(int)", b, "aSlot(int)" );
>>     a.aSignal = &b.aSlot;
>>
>>
>>     a.aSignal(10);    // -> writes "signal triggered 10"
>> }
>>
>>
>>   Bradley
>>
>>
> 
> ok for one, in your example you have a 1 to 1 relationship between 
> signals and slots; of course this can be done with built in arrays. but 
> i think the key feature is that A doesnt have to know anything about B, 
> only that its a qtObject - this provides a whole other level of 
> anonymous polymorphism. you no longer care about interfaces or base 
> classes, just if an object has a particular slot. i suppose the key 
> element missing then would be class introspection.
> 
> yeah i found S&S rather silly before i used them, but they quickly 
> become powerful tools. it seems especially useful in gui code which is 
> heavily event driven. then you have really slick tool integration. many 
> mundane tasks are eliminated. you end up just connecting various signals 
> to slots in a rich class hierarchy to enable various behaviors.....



More information about the Digitalmars-d mailing list