Chinaunix首页 | 论坛 | 博客
  • 博客访问: 34776
  • 博文数量: 23
  • 博客积分: 1415
  • 博客等级: 上尉
  • 技术积分: 235
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-20 09:41
文章分类
文章存档

2011年(1)

2010年(2)

2009年(20)

我的朋友

分类:

2009-09-27 00:45:11

用glade3, 设置界面, 保存为file.glade, 然后用gtk-builder-convert命令转成file.xml

Glib::RefPtr<Gt::Builder> refBuilder = Gtk::Builder::create();

try
{
refBuilder->create_from_file("file.xml")
}
catch(const Blib::FileError& ex)
{
    std::cerr << "FileError" << ex.what() <<std::endl;
    return 1;
}
catch(const Gtk::BuilderError& ex)
{
    stdd::cerr << "BuilderError: " << ex.what() << std::endl;
    return 1;
}

Gtk::Window* pWindow = 0;
refBuilder->get_widget("widget_id_in_xml_file", pWindow));
if(pWindow)
{
    Gtk::Button* pButton = 0;
    refBuilder->get_widget("widget_id", pButton);
    if(pButton)
    {
pButton->signal_clicked().connect(sigc::ptr_fun(on_button_clicked));
    }
}

static
void on_button_clicked()
{
  if(pDialog)
  {
    pDialog->hide();
  }
}


但是如果把界面设置放在xml文件里, 同时, 把相应的代码, 比如回调函数放在继承的类里, 可以使用refBuilder->get_widget_derived("widget_id", pWidget)。 这时, 继承的类必须有:
 DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr& refGlade)
: Gtk::Dialog(cobject) //Calls the base class constructor

这时, 相应的文件有: file.xml, deriveddialog.cpp, deriveddialog.h。

比如在file.xml中有这样一行:

要注意的是在xml文件中类的名字是C类型的, 这里的GtkDialog, 对应在gtkmm中其实是Gtk::Dialog。

而在deriveddialog.h中:

class DerivedDialog: public Gtk::Dialog //和xml文件中对应为GtkDialog
{
public:
DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refGlade);

//
----
}


那么在deriveddialog.cpp中:

DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refGlade)
: Gtk::Dialog(cobject),
  m_refGlade(refGlade),
  m_pButton(0)
{
  //Get the Glade-instantiated Button, and connect a signal handler:


  m_refGlade->get_widget("quit_button", m_pButton);
  if(m_pButton)
  {
    m_pButton->signal_clicked().connect( sigc::mem_fun(*this, &DerivedDialog::on_button_quit) );
  }
}


最后,在main.cc中:

DerivedDialog* pDialog = 0;
  refBuilder->get_widget_derived("DialogBasic", pDialog);
  if(pDialog)
  {
    //Start:

    kit.run(*pDialog);
  }


阅读(601) | 评论(0) | 转发(0) |
0

上一篇:Gtk::Container

下一篇:Gtk::Label

给主人留下些什么吧!~~