How to check the state of a Gtk::Radiomenuitem object?

Asked

Viewed 28 times

0

I am 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: when invoking the 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 incorrectly? How can I correctly check the status of a Radiomenuitem?

1 answer

0

The solution to the problem was to use a wrapper from the sigc++ library itself. The method sigc::bind serves to pass extra arguments and serves exactly for 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 what the current item is and modified 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
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.