销毁gtkmm消息对话框

destroy gtkmm message dialog?

本文关键字:对话框 消息 gtkmm 销毁      更新时间:2023-10-16

我使用的是gtkmm 3.0.1,在用户单击按钮后创建Gtk::MessageDialog对象以销毁对话框时,我看不到任何选项。我发现销毁消息对话框的唯一方法是在辅助函数中调用它,但我觉得这是可以避免的。文档中没有提到销毁它的方法,只提到由用户销毁它

这是我的代码:

#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;
}

问题是您已经在int main中的堆栈上创建了Gtk::MessageDialog。由于该函数在程序完成之前不会退出,因此MessageDialog将挂起。

少数选项:

1.)使用后隐藏对话框,当int main退出时,它将被销毁。

2.)新建然后删除。

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.)在它自己的函数或块中创建它,这样当该作用域退出时,它就会被销毁。

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...
}