Gtkmm盒子没有被移除

时间:2018-05-04 16:03:07

标签: c++ gtk gtkmm

我正在使用gtkmm创建一个应用程序,在那里我创建了许多Gtk :: Box,然后根据需要将它们显示在屏幕上。这些框显示Gtk :: Entry小部件以从用户获取信息以及其他小部件。

我的问题是,当我尝试"删除"这些盒子,他们似乎实际上没有被删除,所以他们占用内存导致我的应用程序崩溃。我不确定它为什么这样做,但我觉得有趣的是:每次我的" remove_widget"执行的函数(删除我要删除的Gtk :: Box),我有一个std :: cout语句,显示" Container Removed"。每次,它理论上应该只删除容器元素(Gtk :: Box)。但随后每次运行时," Container Removed"陈述翻倍。所以它第一次显示一次,下次显示两次,然后是四次,然后是八次,等等。

无论如何,我想要做的核心是在窗口中添加一个特定的框,从中获取信息,然后在我添加不同的Gtk :: Box时销毁它。我不知道这种方法是否是最好的方法,但就目前而言,我是如何实现这一目标的。无论如何,这里是给你一个想法的代码。发生了什么,我加载了MainWin,它创建了一个新窗口。然后MainWin将Event_Controller框打包到其中,Event_Controller处理显示我想为用户显示的其他框。 on_new_flight_clicked()处理显示第一个框,on_next_clicked处理显示其余框。

class MainWin : public Gtk::Window {
public:
  MainWin();
  virtual ~MainWin();

protected:
  // signal handler
  void on_button_clicked(Glib::ustring data);

  // Flight Data Object
  Flight flight;
  // Member widgets:
  Gtk::Box main_container_box;  // Holds everything in the window
  Gtk::Button new_comp; // Allows the user to create a new competition
  Gtk::Button go_home;  // Allows the user to return to the main 
  screen
  Event_Controller* new_event;
};

class Event_Controller : public Gtk::Box {
public:
  Event_Controller(Flight& tflighto);
  virtual ~Event_Controller();

protected:
  Flight* flight;
  Gtk::Box* container_box;
  void on_new_flight_clicked();
  void on_next_clicked(Glib::ustring data);
  void remove_widget();

  Gtk::Button next; // This will appear at every step of the create a 
flight sequence, except the end, taking the user to the next step
};

void Event_Controller::on_new_flight_clicked()
{
  container_box = new Gtk::Box; // Create a new container box
  add(*container_box); // Add it to the box
  container_box->pack_start(*Gtk::manage(new Flight_Letter(flighto)), 
Gtk::PACK_EXPAND_WIDGET); // Pack the first form into the container 
box
  add(next); // Pack the next button outside the container box
  show_all_children();  // Show everything
}

void Event_Controller::remove_widget()
{
  // Remove everything from Event_Controller, this is where I think* things go wrong
  remove(*container_box);
  delete container_box;
  container_box = nullptr;
  cout << "Container Removed..." << endl;

  remove(next);

  container_box = new Gtk::Box; // Create the container box
  add(*container_box);
}

void Event_Controller::on_next_clicked(Glib::ustring data)
{
  int figures_loop = 0;
  // This will show the Flight_Number form. Here is the general model 
for what we do in this function. This function
  // gets passed an argument, and that allows us to remove the form we 
need to and bring up a new form.
  if(data == "first")
  {

    // First we remove the previous form and the container
    remove_widget();
    // Then we set next to send a message to this function when it's 
clicked, so that it knows what form to bring up next
    next.signal_clicked().connect(sigc::bind<Glib::ustring> 
(sigc::mem_fun(*this, &Event_Controller::on_next_clicked), "first"));
    // Create the new form
container_box->pack_start(*Gtk::manage(new 
Figure_Number(num_of_figures)), Gtk::PACK_EXPAND_WIDGET);

    // Pack the next button
    pack_start(next);
    // Show all the widgets
    show_all_children();
  }
// There is more to this function, I just left it out for brevity sake
}

感谢您提前提供任何帮助,如果有任何不明之处或您有任何建议/疑虑/意见,请随时发表评论。

0 个答案:

没有答案