Previous Next
Alia Widgets

Signals And Slots

Jordan CM Wambaugh

Table of Contents


Alia's signals and slots are an implementation of the Observer Design Pattern. The signals and slots mechanism allows objects to be notified of events in other objects, without the observed object having to know anything about the observing objects. This allows objects to communicate with each other, without any tight coupling.

A SIGNAL is EMITTED from an object whenever a specific event occurs within the object. Other objects may CONNECT to that specific SIGNAL if they wish. When an object connects to a signal of another object, it must specify a SLOT method for the emitted signal to be sent to. This slot method will be called each time the signal it is connected to is emitted.

An example of the signals and slots mechanism can be found in the simple ALoginWidget widget. (ALoginWidget is a simple widget that allows a user to enter a user name an a password) If I am writing a program and I want to use the ALoginWidget in my program, I need to know when the user fills out the form and hits the submit button, so that my application can log them into the system. ALoginWidget emits a signal called 'loginPushed' when the user clicks the submit button. All my application has to do, is connect a slot to the 'loginPushed' signal.

  1. class testWidget extends AWidget {
  2.     function __construct(){
  3.         parent::__construct();
  4.         //set up our simple layout
  5.         $layout new ASimpleLayout($this);
  6.         $this->setLayout($layout);
  8.         //create our login widget
  9.         $loginWidget new ALoginWidget();
  11.         //add our login widget to the layout
  12.         $layout->addWidget($loginWidget);
  14.         //Connect the 'loginPushed' signal to our loginUser slot
  15.         //now whenever the user clicks the login button, out loginUser method will be called,
  16.         //and passed the login information (The loginUser signal sends the login info when emitted)
  17.         Alia::connect($loginWidget,'loginPushed',$this,'loginUser');
  18.     }
  20.     //just send a simple alert box with the username and password
  21.     function loginUser($user,$password){
  22.         Alia::sendJScript(AJScript::alert("$user logged in with password $password"));
  23.     }
  24. }
Previous Next
Alia Widgets

Documentation generated on Fri, 11 Jul 2008 13:28:37 -0400 by phpDocumentor 1.4.2