1

I'm working on a gtkmm program and I would like to have a button that, when pressed, changes a given variable. For intensive purposes lets say I can't make this variable global. Is it still possible to call something like:

m_button2.signal_clicked().connect(sigc::ptr_fun(&on_player_button_clicked));

where on_player_button_clicked is the method that's called when button2 is pressed, but also pass it a parameter. So I want my button, when pressed, to call my method like this:

int parameter
m_button2.signal_clicked().connect(sigc::ptr_fun(&on_player_button_clicked(parameter)));

If this is not possible, is there any way for me to get my button to change this variable without making the variable global?

4

1 回答 1

1

在设计方面,至少是OO设计,首先决定谁拥有变量。

然后,想法是向该所有者发送一条消息,“所有者先生,我命令您更改变量”。您可以通过发出“variable_needs_change”信号并让所有者收听它来做到这一点,或者给所有者一个设置器方法并让按钮使用它(通过让回调函数访问所有者的引用)。

但抛开它,有一个简单的绑定选项。这是简单明了的解决方案:使用 sigc::bind 添加参数。如果我没记错的话,在 gtkmm 书中有一个例子(也可能在 sigc++ 教程中)。

于 2013-05-30T08:20:44.560 回答