gtkmm更改切换按钮状态而不调用信号

时间:2017-03-24 21:33:18

标签: radio-button gtk signals togglebutton gtkmm

我正在尝试将多个Gtk :: ToggleButton作为Gtk :: RadioButton。当按下其中一个按钮时,其他按钮关闭。

如果Gtk :: ToggleButton本身没有处理切换(按下或未按下),那么就像创建一个switch语句一样简单。

所以,我打算把它作为常规按钮切换,调用signal_clicked()。connect()调用函数set_active(true / false),使按钮看起来按下或不按下。

这是我正在尝试做的一个例子:

点击按钮时的事件调用:

//enum {infoState, artState, editState, userState, exitState}; is initialised in header 

artButt.signal_clicked().connect(sigc::bind<short int>(sigc::mem_fun(*this, &fooclass::toggleButton), artButt));  
editButt.signal_clicked().connect(sigc::bind<short int>(sigc::mem_fun(*this, &fooclass::toggleButton), editButt));

切换按钮:

void fooClass::toggleButton()
{
//oldState and enum {infoState, artState, editState, userState, exitState}; are initialised in header
if(oldState != newState)
{   
    //disable old togglebutton
    switch (oldState)
        {
            case infoState:
                infoButt.set_active(false);
            break;
            case artState:
                artButt.set_active(false);
            break;
            case editState:
                editButt.set_active(false);
            break;
            case userState:
                userButt.set_active(false);
            break;
        }

        //enable new one
        switch (newState)
        {
            case infoState:
                userButt.set_active(false);
            break;
            case artState:
                artButt.set_active(true);
            break;
            case editState:
                editButt.set_active(true);
            break;
            case userState:
                userButt.set_active(true);
            break;
        }
        oldState = newState;
}    
}    

2 个答案:

答案 0 :(得分:0)

好的,这是我的解决方法。可能有更好的方法,所以如果你知道在这里发布任何信息,请点击:

首先使用Gtk :: Button代替Gtk :: ToggleButton。

其次,而不是set_active()使用set_state_flags()。当你希望它被启用时,Gtk :: STATE_FLAG_CHECKED和你希望它被禁用的GTK :: STATE_FLAGS_NORMAL。

所以,这就是代码的样子:

void Window::changeState()
{
    if(oldState != state)
    {
        //delete old state
        switch (oldState)
        {
            case infoState:
                infoButt.set_state_flags(Gtk::STATE_FLAG_NORMAL);
            break;
            case artState:
                artButt.set_state_flags(Gtk::STATE_FLAG_NORMAL);
            break;
            case editState:
                editButt.set_state_flags(Gtk::STATE_FLAG_NORMAL);
            break;
            case userState:
                userButt.set_state_flags(Gtk::STATE_FLAG_NORMAL);
            break;
        }
        //move to new state
        switch (state)
        {
            case infoState:          
                infoButt.set_state_flags(Gtk::STATE_FLAG_CHECKED);
            break;
            case artState:
                artButt.set_state_flags(Gtk::STATE_FLAG_CHECKED);
            break;
            case editState:
                head.editButt.set_state_flags(Gtk::STATE_FLAG_CHECKED);
            break;
            case userState:
                userButt.set_state_flags(Gtk::STATE_FLAG_CHECKED);
            break;

            case exitState:
                close();
            break;
        }

        show_all_children();
        oldState = state;

    }
}

答案 1 :(得分:0)

直接使用for (size_t i = 0; i != m; ++i) { for (size_t j = 0; j != n; ++j) { free(tas[i][j]); // free the string } free(tas[i]); // free the row } free(tas); // free the table 。使用Gtk::RadioButton属性,您可以使它们看起来像常规切换按钮。