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

今天写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编程有兴趣的朋友可以和我探讨一下这个问题哦,呵呵。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值