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;
}
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...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With