409. How to disconnect a signal?

Problem

This is in response to Manar's post titled "How to disconnect an event?"

He has set up a button-press-event and want to disconnect the signal as shown below:

How to disconnect a signal?


Solution


Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
45   
46   
49   
50   
51   
52   
53   
54   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 175);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Disconnecting a signal");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0);
$vbox->pack_start(new GtkLabel(), 0);

$vbox->pack_start(new GtkLabel('Click on the blue sqaure once. '.
    'It will change to green.'), 0);

$signal_id = setup_colorbox('#0000ff', 'blue', $vbox); // note 1

$window->show_all();
Gtk::main();


function setup_colorbox($color, $label, $vbox) {
    $hbox = new GtkHBox();
    $hbox->set_size_request(30, 30);
    $eventbox = new GtkEventBox();
    $eventbox->add($hbox);
    $eventbox->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse($color));

    $signal_id = $eventbox->connect('button-press-event', 
        'on_button_press'); // note 2

    $hbox = new GtkHBox();
    $vbox->pack_start($hbox, 0);
    $hbox->pack_start(new GtkLabel());
    $hbox->pack_start($eventbox, 0);
    $hbox->pack_start(new GtkLabel());

    return $signal_id;
}

function on_button_press($eventbox, $event) {
    $eventbox->modify_bg(Gtk::STATE_NORMAL, 
        GdkColor::parse('#00ff00')); // note 3

    global $signal_id;
    $eventbox->disconnect($signal_id); // note 4
}

?>

Output

How to disconnect a signal?

 

Explanation

  1. Set up the blue color box. Note that here we store the signal handler id in the variable $signal_id.
  2. Set up the button-press-event event. Don't forget to take note of the signal handler id.
  3. Change the color box from blue to green.
  4. Disconnect the button-press-event signal.

Related Links

Add comment


Security code
Refresh