How to check the status of a Gtk :: RadioMenuItem object?

0

I'm building a dynamic menu using gtkmm. The menu is built as follows:

Gtk::RadioButtonGroup appRadioGroup;
bool groupInitialized = false;

Gtk::Menu *driverSubMenu = Gtk::manage(new Gtk::Menu);
driverSubMenu->set_visible(true);

Inicio Loop...

Gtk::RadioMenuItem *appMenuItem = Gtk::manage(new Gtk::RadioMenuItem);
appMenuItem->set_visible(true);
appMenuItem->set_label("Nome da opção");

if (!groupInitialized) {
    appRadioGroup = appMenuItem->get_group();
    appMenuItem->set_active(true);
    groupInitialized = true;
} else {
    appMenuItem->set_group(appRadioGroup);
}

appMenuItem->signal_toggled().connect([this, &appMenuItem]() {
    this->onApplicationSelected(appMenuItem);
});

driverSubMenu->append(*appMenuItem);

Fim Loop...

The method that treats the signal is:

void onApplicationSelected(Gtk::RadioMenuItem *item) {
    if(!item) {
        return;
    }

    if(item->get_active()) {
        std::cout << "Item is active " << std::endl;
    } else {
        std::cout << "Item is not active " << std::endl;
    }

}

My problem is with this signal handler. When I try to invoke any method in the variable item I get critical errors from GTK +. For example: invoking method get_active() I get the following error:

Gtk-CRITICAL **: gtk_check_menu_item_get_active: assertion 'GTK_IS_CHECK_MENU_ITEM (check_menu_item)' failed

What am I doing wrong? How can I correctly check the status of a RadioMenuItem?

    
asked by anonymous 05.01.2018 / 22:56

1 answer

0

The solution to the problem was to use a wrapper from the sigc ++ library itself. The sigc::bind method is used to pass extra arguments and serves exactly the purpose I was needing.

To solve, I changed the code to look like this:

appMenuItem->signal_toggled().connect(sigc::bind<Glib::ustring>(
                    sigc::mem_fun(this, &DRI::GUI::onApplicationSelected),
                    "Item selecionado"
            ));

I added a property in the class to know the current item and modified the handler to look like this:

void onApplicationSelected(Glib::ustring item) {
    if(this->estadoAtual != item) {
        this->estadoAtual = item;
        // FAZ ALGUM OUTRO CÓDIGO QUE EU QUERO
    }
}
    
06.01.2018 / 12:20