Having tried and failed to enable and disable a button in my application I thought I would share my findings. Comments and feedback welcome.
I was using the buttons superclass method “set_action_name” to control the button. If there is no method call to the buttons Gtk.Actionable interface “set_action_name”. Then “set_sensitive” is enough.
If the button is associated with a Gtk.SimpleAction then a call to Gtk.Actionable interface “set_action_name” will have been set. The consequences being: trying to control the button using “set_sensitive” fails. However Gtk.SimpleAction has a method “set_enabled” which also enables and disables the signal but it interacts with the buttons set_sensitive setting.
Below is a table identifying the combinations that work and those that don’t:
Button.set_sensitive(boolean) | SimpleAction.set_enabled(boolean) | Button State: |
---|---|---|
true | na | Active |
false | na | Inactive |
true | true | Active |
true | false | Active |
false | true | Active |
false | false | Inactive |
Below is some pseudocode to demonstrate the solution:
// The button setup
button = Gtk.Button();
button.set_action_name("app.button_cb");
button.set_sensitive(false); // trying to control the button here will FAIL.
// The simple action setup
simple_action = GLib.SimpleAction("app.button_cb", null);
add_action(simple_action);
simple_action.set_enabled(false); // use this to control the button.
simple_action.active.connect(to_the_callback_function);