0

当我的程序未连接到数据库时,我正在尝试将信息栏添加到对话框中。但是,如果我关闭对话框并再次打开它,则每次有一个新按钮时,信息栏都会显示。这是我的代码:

窗口.hpp

class Window: public Gtk::Window {
private:
/// more things
  Gtk::InfoBar info;
  Gtk::Container* infoBarContainer;
  Gtk::Label info_label;
protected:
  void onConnectedBody(void);
  void onNotConnectedBody(void);
  void onInfoBarResponse(int response);
  void onMenuDbConnect(void);
  void onMenuDbDisconnect(void);
public:
  /// more things...
};

窗口.cpp

Window::Window(
) :
 infoBarContainer(0),
 info_label("")
{
  // more things
  info.add_button(Gtk::Stock::OK, 0);
  infoBarContainer = dynamic_cast<Gtk::Container*>(info.get_content_area());
  if( infoBarContainer ) infoBarContainer->add(info_label);      
}

Window::onMenuDbConnect(){
  if( controller->Connected() ) {
    onConnectedBody();
  } else {
    onNotConnectedBody();
  }
}

Window::onConnectedBody(){
 Gtk::Dialog dialog("Connected to database",true,true);
  dialog.set_resizable( false );
  dialog.set_has_separator(true);
  Gtk::Button close;
  Gtk::VBox* vbox = dialog.get_vbox();
  Gtk::Label label;
  label.set_markup("<b>Already connected.</b>"); 
  vbox->pack_start(label,false,false);
  close.set_label("Close");
  close.signal_clicked().connect(sigc::mem_fun(*this,&Window::onButtonClose));
  dialog.set_size_request(250,80);
  vbox = 0;
  close.set_can_default( true );
  dialog.add_action_widget(close,1);
  close.grab_default();

  dialog.show_all_children();
  dialog.run();    
}
Window::onNotConnectedBody(){
 Gtk::Dialog dialog("Connect to database",true,true);
 dialog.set_resizable( false );
 dialog.set_has_separator(true);
 Gtk::Button close;
 Gtk::VBox* b = dialog.get_vbox();

 info.signal_response().connect(sigc::mem_fun(*this,&Window::onInfoBarResponse) ); 
 info_label.set_text("Remember to fill all fields");
 b->pack_start(info,Gtk::PACK_SHRINK);

 Gtk::Label label;
 label.set_markup("<b>Database:</b>");
 Gtk::Label label_host;
 label_host.set_markup("<b>Host:</b>");
 Gtk::Label label_user;
 label_user.set_markup("<b>User:</b>");  
 Gtk::Label label_pass;
 label_pass.set_markup("<b>Pass:</b>");  

 label.set_justify(Gtk::JUSTIFY_LEFT);
 label_host.set_justify(Gtk::JUSTIFY_LEFT);
 label_user.set_justify(Gtk::JUSTIFY_LEFT);
 label_pass.set_justify(Gtk::JUSTIFY_LEFT);

 entry.set_max_length(10);
 entry.set_text("Figures");
 entry_host.set_text("localhost");
 label.set_markup("<b>Database:</b>");

 b->pack_start(label,false,false);
 b->pack_start(entry,false,false);
 b->pack_start(label_host,false,false);
 b->pack_start(entry_host,false,false);
 b->pack_start(label_user,false,false);
 b->pack_start(entry_user,false,false);

  b->pack_start(label_pass,false,false);
 b->pack_start(entry_pass,false,false);

 close.set_label("Connect");
 close.signal_clicked().connect(sigc::mem_fun(*this,&Window::onButtonDbConnect));
 close.set_can_default( true );
 dialog.add_action_widget(close,1);
 close.grab_default();
 dialog.set_size_request(250,250);
 dialog.show_all_children();
 dialog.run();
}
void Window::onInfoBarResponse(int response) {
  info.hide();
}

所以,我想要的是在我进入 onMenuDbConnect 并且尚未连接到数据库时显示信息栏。如果已连接,则不会显示任何信息栏。我的程序的其他组件,如 DB 控制器,运行良好。

有任何想法吗?谢谢。

4

0 回答 0