Joel
Joel

Reputation: 2035

What would be gtkmm's version of g_signal_emit or g_signal_emit_by_name?

I'm running a timeout function in my program background and I'm trying to emit a delete-event signal from Gtk::Button, this is the code snippet in my constructor:

// Glib::SignalProxy1<bool,GdkEventAny*> m_deleteSlot;
// m_deleteSlot =
signal_delete_event().connect (sigc::mem_fun (*this, &AlarmUI::my_delete_event));
m_timeout_connection = Glib::signal_timeout().connect_seconds(sigc::mem_fun(*this, &AlarmUI::cb_my_tick), 1);`

Now, the method:

bool AlarmUI::my_delete_event (GdkEventAny *event) {
if (m_timeout_connection.connected ()) {
    // show messagebox here
    return true;
} else {
    // bye bye
    return false;
}
}

Now, when the user clicks in the quit button, I want to emit the delete-event signal. Question: How do you emit signals in gtkmmm like in C g_signal_emit or g_signal_emit_by_name?

void AlarmUI::on_button_quit () {
// m_deleteSlot.emit (); ???
}

update1:

Glib::RefPtr<Gtk::Application> app = Gtk::Application::create (argc, argv, PACKAGE);
Glib::RefPtr<Gtk::Builder> refBuilder = Gtk::Builder::create ();
try { 
    refBuilder->add_from_file (UI_PATH);
}
catch (const Glib::FileError& ex) {
    std::cout << "FileError: " << ex.what() << std::endl;
    return 1;
}
catch (const Gtk::BuilderError& ex) {
    std::cout << "BuilderError: " << ex.what() << std::endl;
    return 1;
}
catch(const Glib::MarkupError& ex)
{
    std::cout << "MarkupError: " << ex.what() << std::endl;
    return 1;
}
AlarmUI *ui = 0;
refBuilder->get_widget_derived ("window1", ui);
if (ui) {
    ui->show_all ();
    app->run (); // The window doesn't show
}
delete ui;

Upvotes: 5

Views: 859

Answers (2)

Andy Broad
Andy Broad

Reputation: 1

Whilst there isn't a direct method call in the Gtk::Widget if you need to emit an existing signal, perhaps as part of a subclass of a widget where you want to change the behaviour you can do something like:

void SubClass::EmitSignal() {
    GtkWidget *gobj = this->gobj();
    g_signal_emit_by_name(gobj, "changed"); 
}

Upvotes: 0

murrayc
murrayc

Reputation: 2123

It's not generally wise to emit widget signals from outside of the widget. That's interfering with the internals of the widget's implementation. If you want to hide the window you can call thewindow.hide() and if you want to destroy it you can delete it. Or you can directly do whatever else you wanted to trigger indirectly by emitting the delete-event signal.

Upvotes: 1

Related Questions