3

In lablgtk, I have an Expander widget to which I added a button as part of its label, as in this picture:

GTK expander with a custom label_widget containing a button

However, I'm unable to capture clicks to this button, since all of them seem to be captured by the Expander object itself.

I even tried adding an event box between the expander and the button (similarly to what is recommended for clickable GLabels), but that did not help.

Is there a way to ensure the button receives the signal?

Here's a self-contained example to illustrate this: clicking on the button only prints expander activated!, but never button clicked!.

let main () =
  (* create expander, a label_widget, and a button *)
  let expander = GBin.expander () in
  let expander_label = GMisc.label ~text:"expander" () in
  let expander_label_widget = GPack.hbox () in
  let button = GButton.button ~stock:`OK () in

  (* add button to label_widget, and label_widget to expander *)
  expander_label_widget#add expander_label#coerce;
  expander_label_widget#add button#coerce;
  expander#set_label_widget expander_label_widget#coerce;

  (* add events for both the button and the expander *)
  ignore (button#connect#clicked
            ~callback:(fun () ->
                Format.printf "button clicked!@."));
  ignore (expander#connect#activate
            ~callback:(fun () ->
                Format.printf "expander activated!@."));

  (* create window and add expander *)
  let window = GWindow.window () in
  window#add expander#coerce;
  ignore (window#connect#destroy
            ~callback:(fun () -> GMain.Main.quit ()));
  (* show result *)
  window#show ();
  GMain.Main.main ()

let () =
  ignore (GtkMain.Main.init ());
  main ()
4

1 回答 1

3

这似乎是 GTK+ 中的一个错误此处描述了一种解决方法,但它使用 C 语言。您必须将以下 OCaml 代码添加到您的 main 函数中。它对我有用。

  GMain.Idle.add (fun () ->
    let dummy = GButton.button ~stock:`OK () in
    expander#set_label_widget dummy#coerce ;
    expander#set_label_widget expander_label_widget#coerce ;
    false) ;
于 2015-09-05T13:39:42.280 回答