今天写Gtkmm程序的时候遇到了一个很奇怪的问题。本来的想法是这样的:当点击主窗口的“连接”按钮之后,主窗口隐藏,“连接中……”对话框显示,如果把“连接中……”对话框关掉的话,主窗口显示。
当点击主窗口的“连接”按钮之后,主窗口隐藏,“连接中……”对话框显示,这一步是成功的,但不知道为什么,一点击“连接中……”对话框的关闭按钮之后,整个程序的结束运行了。代码如下:
MainFrame.cpp
void MainFrame::btnLogin_clicked(){ Gtk::TreeModel::iterator iter = choNetworkCard.get_active(); if(iter) { Gtk::TreeModel::Row row = *iter; if(row) { //Get the data for the selected row, using our knowledge of the tree //model: this->device = row[m_Columns.m_col_id]; this->userName = this->txtUserName.get_text(); this->password = this->txtPassword.get_text(); //this->connect(); this->hide(); cout<<this<<endl; dlgConnecting->run(); } }else std::cout << "invalid iter" << std::endl; } void MainFrame::dlgConnecting_response(int response){ cout<<"return "<<response<<endl; switch(response){ case Gtk::RESPONSE_DELETE_EVENT: cout<<"connecting dialog close."<<endl; cout<<this<<endl; dlgConnecting->hide(); this->show();//似乎这里出了问题 break; default:; } } void MainFrame::dlgConnecting_init(){ dlgConnecting = new Dialog("连接中...", true, true); btnCancel = new Button("取消"); btnDetail = new Button("详细信息"); dlgConnecting->set_size_request(370,150); dlgConnecting->signal_response().connect(sigc::mem_fun(*this, &MainFrame::dlgConnecting_response)); dlgConnecting->get_action_area()->add(*btnDetail); dlgConnecting->get_action_area()->add(*btnCancel); btnDetail->show(); btnCancel->show(); }
main.cpp
#include <gtkmm.h> #include <polkit/polkit.h> #include "MainFrame.h" using namespace Zte; int main(int argc, char **argv){ Main kit(argc, argv); MainFrame window; Main::run(window); return 0; }
MainFrame.cpp
void MainFrame::dlgConnecting_response(int response){ cout<<"return "<<response<<endl; switch(response){ case Gtk::RESPONSE_DELETE_EVENT: cout<<"connecting dialog close."<<endl; cout<<this<<endl; dlgConnecting->hide(); Main::run(*this); break; default:; } }
至于这是为什么,我自己也说不清楚,因为我不知道show()和hide()做了一些什么工作,而且网上关于Gtkmm编程的资料实在是太少啦,有的都是Gtk+的。Gtkmm的代码我也看了一下,但我看它错综复杂的,也不去深究了。
Widget.cc
//…… void Gtk::Widget::on_hide() { BaseClassType *const base = static_cast<BaseClassType*>( g_type_class_peek_parent(G_OBJECT_GET_CLASS(gobject_)) // Get the parent class of the object class (The original underlying C class). ); if(base && base->hide) (*base->hide)(gobj()); } //…… void Gtk::Widget::on_show() { BaseClassType *const base = static_cast<BaseClassType*>( g_type_class_peek_parent(G_OBJECT_GET_CLASS(gobject_)) // Get the parent class of the object class (The original underlying C class). ); if(base && base->show) (*base->show)(gobj()); } //……