Signal fires twice from gtkmm popup list

It has been a while since I used GTK + and the last time I did it in C without using gtkmm and C ++, just like now. Anyway, I have what I think should be an easy problem to solve:

I have a popup menu consisting of a list of radio buttons and when I click one of them I need some action. The code looks like this:

    Gtk::RadioMenuItem::Group group;
    for ( size_t i = 1; i < LH_MAX; ++i )
    {
        Gtk::RadioMenuItem* pItem = new Gtk::RadioMenuItem( group, names[i], names[i] );
        pItem->set_name( names[i] );
        pItem->signal_activate().connect( sigc::mem_fun(*this, &MyClass::on_item_activated) );
        pItem->show();
        m_Menu.append( *Gtk::manage(pItem) );
    }

      

The only problem I see is that it MyClass::on_item_activated

gets called twice when a previously selected radio button is selected in the menu. It is called only once, when an already selected radio button is clicked.

My guess is that the first shooting is to say "something is no longer activated" and the second is to activate the new button. Am I right or wrong, the question is the same: what is the best way to have my handler do one action per click? Either I need a handler to call only once, or I need to check something internally to see if the callback is "duplicated" or not.

0


source to share


4 answers


You can use sigc :: bind to feed an element as an argument to a callback function.

pItem->signal_activate().sigc::bind(sigc::mem_fun(*this,&MyClass::on_item_activated),pItem));

      



Then you can use item-> get_active () in the callback to only respond to activation.

      void MyClass::on_item_activated(Gtk::RadioMenuItem* item) {
        if (item->get_active()) {
               // Do some stuff
        }
    }

      

+1


source


Which I do too, connect to signal_toggled()

and check if it is true get_active()

.



+1


source


I don't know exactly what you are trying to accomplish (or what is MyClass and what base classes it inherits from), but connecting to signal_toggled()

might be more useful thansignal_activate()

0


source


/ Agree with Johannes. Check if the element is activated when receiving a signal.

0


source







All Articles