销毁gtkmm消息对话框?

cel*_*eet 4 c++ messagebox gtkmm

我使用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;

}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ark 6

问题是你已经在Gtk::MessageDialog堆栈中创建了你的int main.由于该功能不会退出,直到你的程序MessageDialog挂起.

几个选项:

1.)完成后隐藏对话框,当主要退出时它将被销毁.

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;    
Run Code Online (Sandbox Code Playgroud)

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...
}
Run Code Online (Sandbox Code Playgroud)

  • 小心这种方法.虽然它可能会起作用,但在局部变量上手动调用析构函数会导致麻烦:http://www.parashift.com/c++-faq/dont-call-dtor-on-local.html. (2认同)