【问题标题】:Close main window ffrom messagebox using gtkmm / c++使用 gtkmm / c++ 从消息框关闭主窗口
【发布时间】:2020-09-13 16:07:18
【问题描述】:

如果我点击确定按钮,我想从一个消息框中关闭主窗口。

class usb_boot : public Gtk::Window{

public:
    usb_boot();

来自消息框

我试过了

void usb_boot::creation(){
//Gtk::MessageDialog dialog(*this, dropdownList.get_active_text());
std::string message("Format : " + type);
Gtk::MessageDialog *dialog = new Gtk::MessageDialog("Resume", true, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);
dialog->set_title("Resume");
dialog->set_message(dropdownList.get_active_text());
dialog->set_secondary_text(message);
dialog->set_default_response(Gtk::RESPONSE_YES);
int result = dialog->run();

switch(result){

    case(Gtk::RESPONSE_YES):{

        std::cout << "next program" << std::endl;
        delete dialog;// ok work
        usb_boot().close();//compile but doesn't close main window
        break;
    }

如何关闭主窗口?

【问题讨论】:

    标签: c++ gtkmm3


    【解决方案1】:

    您应该尽可能避免使用原始的new/delete(例如这里)。对于消息对话框,您可以使用简单的范围:

    #include <iostream>
    #include <gtkmm.h>
    
    class MainWindow : public Gtk::ApplicationWindow
    {
    
    public:
        MainWindow() = default;
    
    };
    
    int main(int argc, char **argv)
    {
        auto app = Gtk::Application::create(argc, argv, "so.question.q63872817");
        
        MainWindow w;
        w.show_all();
        
        int result;
        // Here we put the dialog inside a scope so that it is destroyed
        // automatically when the user makes a choice (you could do it
        // inside a function instead of a free scope):
        {
            Gtk::MessageDialog dialog(w, "Message dialog", true, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);
            dialog.set_title("Title");
            dialog.set_message("Primary message");
            dialog.set_secondary_text("Secondary message");
            dialog.set_default_response(Gtk::RESPONSE_YES);
            
            result = dialog.run();
            
        } // Here the dialog is destroyed and closed.
        
        if(result == Gtk::RESPONSE_YES)
        {
            std::cout << "Closing main window..." << std::endl;
            //MainWindow().close(); // Will not work!
            w.close();
        }
    
        return app->run(w);
    }
    

    另外,在您的代码中,您调用了usb_boot().close(),但请注意usb_boot 之后的额外括号。这会构造一个新的usb_boot 对象(因为您调用了构造函数)并立即关闭它。在上面的示例中,我调用了w.close(),而不是MainWindow().close()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多