如何从Gtk :: Window获取派生对象的析构函数调用

问题描述 投票:0回答:1

我想从Gtk::Window派生,并希望从该对象创建一个独立的窗口。如果用户关闭该窗口,如何实现将调用派生对象的析构函数。

目前,我的代码从未调用过独立窗口的析构函数!

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


class ExampleWindow : public Gtk::Window
{
    protected:

        //Child widgets:
        Gtk::Box m_VBox; 
        Gtk::Label m_Label1;
        std::string mytext;

    public:
        ExampleWindow(const std::string& text_):
            m_VBox{ Gtk::ORIENTATION_VERTICAL }
            ,m_Label1{ text_ }
            ,mytext{ text_ }
        {
            set_title("Example");
            set_border_width(10);
            set_default_size(400, 200);

            add(m_VBox);

            m_VBox.pack_start( m_Label1 );

            show_all_children();
        }

        virtual ~ExampleWindow()
        {
            // Not called for the stand alone win while closing it. How to achieve that?
            std::cout << "Destructor called for " << mytext << std::endl;
        }

};  


int main(int argc, char *argv[])
{
    auto app = Gtk::Application::create(argc, argv, "some.base");

    ExampleWindow window{ "First Window" };

    // Create the same window as free window ( toplevel )

    ExampleWindow* win2 = new ExampleWindow("Stand Alone Win");

    win2->show();   // How can I desruct this window, if a user closes it?

    //Shows the window and returns when it is closed.
    return app->run(window);
}
c++ destructor gtkmm
1个回答
0
投票

您动态分配win2,但此后再也没有释放内存。

delete返回后在win2上调用Gtk::Application::run()

delete win2;
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.