Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

destroy gtkmm message dialog?

I am using gtkmm 3.0.1 and I do not see an option when creating a Gtk::MessageDialog object to destroy the dialog after the user has clicked a button. The only way I found out to destroy a message dialog is to call it in a secondary function, but I feel that this has the possibility of being avoided. The documentation mentions no method of destroying it, only mentions that it is up to the user to destroy it.

Here's my code:

#include <gtkmm.h>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {

    Gtk::Main kit(argc, argv);
    Gtk::Window client;

    Gtk::MessageDialog dialog("Info", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);
    dialog.set_secondary_text( "Dialog");
    dialog.set_default_response(Gtk::RESPONSE_YES);
    dialog.run();

    cout << "dialog is still open, needs to be destroyed at this point." << endl;

    Gtk::Main::run(client);

    return EXIT_SUCCESS;

}
like image 901
cellsheet Avatar asked Mar 23 '23 14:03

cellsheet


1 Answers

The problem is you've created your Gtk::MessageDialog on the stack in int main. Since that function won't exit until your program does your MessageDialog hangs around.

Few options:

1.) Hide the dialog when done with it, it'll be destroyed when int main exits.

2.) New it then delete it.

Gtk::MessageDialog* dialog = new Gtk::MessageDialog("Info", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);
dialog->set_secondary_text( "Dialog");
dialog->set_default_response(Gtk::RESPONSE_YES);
dialog->run();
delete dialog;    

3.) Create it in it's own function or block so it'll be destroyed when that scope exits.

void showDialog() {
    Gtk::MessageDialog dialog("Info", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);
    dialog.set_secondary_text( "Dialog");
    dialog.set_default_response(Gtk::RESPONSE_YES);
    dialog.run();
}

int main(int argc, char *argv[]) {
    etc...
    showDialog();
    Gtk::Main::run(client);
    etc...
}
like image 195
Mark Avatar answered Apr 06 '23 02:04

Mark