您的位置:首页 > 其它

Gtkmm 主窗口与其对话框之间的切换

2011-09-22 01:01 423 查看
今天写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();
}


经过我用gdb调试之后,发现调用this->show()之后,程序重新运行到main函数里,接着程序就结束了

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;
}


这样一来,我就更加不理解了。不过,我想,既然你重新运行到main里,那我就让你在show的时候让主窗口再run一次,于是我就有了把this->show()改为Main::run(*this)的想法。修改后的代码如下:

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());
}
//……


对Gtkmm编程有兴趣的朋友可以和我探讨一下这个问题哦,呵呵。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: