当我的程序未连接到数据库时,我正在尝试向对话框添加信息栏。但是,每当有新按钮时,信息栏会显示我是否关闭对话框并再次打开它。这是我的代码:
Window.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...
};
Window.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并且尚未连接到数据库时显示信息栏。如果已连接,则不显示任何信息栏。我的程序的其他组件,如数据库控制器,可以很好地工作。
有什么想法吗? Thaanks。