プログラムがデータベースに接続されていないときに、ダイアログに情報バーを追加しようとしています。ただし、ダイアログを閉じて再度開くと、新しいボタンがあるたびに情報バーが表示されます。これが私のコードです:
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 に入ってデータベースにまだ接続されていないときはいつでも、情報バーを表示する必要があります。接続されている場合、情報バーは表示されません。私のプログラムの他のコンポーネント (DB コントローラーなど) は完全に機能します。
何か案は?ありがとう。