仅在选择时才读取GTK无线电按钮信号 [英] Read GTK Radio Button signal only when selected

查看:103
本文介绍了仅在选择时才读取GTK无线电按钮信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GTK的切换信号会在选中单选按钮时触发,但在此之前,它也会在取消选定之前选定的单选按钮时触发。



我工作正常使用单选按钮的GUI,每个按钮代表一组实体。这对切换信号中的第一个触发了对GUI中其他字段的一些不需要的更新 - 我只希望在新选择的按钮触发回调时发生更新。我该如何解决这个信号问题,并将回调函数限制为仅对选择进行操作而不是取消选择?我在编码的类中考虑了一个标志变量,但也许有更多GTK认可的技术。 解决方案

我不认为你只能获得选择信号,但你可以做其他事情。您可以编写信号处理程序,以便获取已切换的按钮(假设您正在为多个按钮重复使用相同的处理程序)。然后你可以检查它的状态,看它是否被选中或取消选择。



这样做的方式是连接适配器:

  / /为每个按钮做这个(这是一个buttona):
buttona.signal_toggled()。connect(
sigc :: bind(
sigc :: mem_fun(* this ,& myclass :: handle_button_toggled),
buttona

);

在你的类中,handle_button_toggled包括button参数:

  myclass {
Gtk :: ToggleButton buttona,buttonb,...
....
void handle_button_toggled(Gtk :: ToggleButton& amp; amp; ; b){
...检查b的状态...
}
}

在C ++ 11中,您可以使用lambda表达式:

  buttona.signal_toggled() .connect([this,& buttona] {
handled_button_toggled(buttona);
});


GTK's "toggled" signal fires when a radio button is selected, but before that, it also fires upon deselection of the previously selected radio button.

I am working with a GUI that uses radio buttons, each representing a group of entities. The first of the pair of "toggled" signals is triggering some unwanted updates to other fields in the GUI -- updates that I only want to happen when the newly selected button triggers the callback. How do I work around this signal and limit the callback function to only operate on selection instead of deselection? I've considered a flag variable within the class I'm coding, but perhaps there is a more GTK-approved technique.

解决方案

I don't think you can only get selection signals, but you can do something else. You can write your signal handler so it gets the button that was toggled (assuming you are reusing the same handler for several buttons). Then you can check its state to see if it was selected or deselected.

The way you do this is to connect with an adapter:

// do this for each button (this one is for "buttona"):
buttona.signal_toggled().connect(
   sigc::bind(
      sigc::mem_fun(*this, &myclass::handle_button_toggled),
      buttona
   )
);

In your class, handle_button_toggled includes the button parameter:

myclass {
  Gtk::ToggleButton buttona, buttonb, ...
  ....
  void handle_button_toggled(Gtk::ToggleButton &b) {
   ...check state of b ...
  }
}

In C++11, you can alternatively use a lambda expression:

buttona.signal_toggled().connect([this,&buttona]{
  handled_button_toggled(buttona); 
});

这篇关于仅在选择时才读取GTK无线电按钮信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆