| 代码: |
| #include "hello-app.hpp" int main (int argc, char *argv[]) { HelloApp app(argc, argv); app.show_window(); return 0; } |
| 代码: |
| #ifndef _HELLO_APP_HPP_ #define _HELLO_APP_HPP_ #include <gtkmm.h> #include <libglademm/xml.h> //v2+++++++++++++++ #include <libglademm/variablesmap.h> /* 连接控件与变量 */ #include <sigc++/functors/slot.h> /* 信号槽 */ //v2--------------- class HelloApp: public Gtk::Main { public: HelloApp(int argc, char *argv[]); ~HelloApp(); void show_window(); protected: Glib::RefPtr<Gnome::Glade::Xml> ref_xml; Gtk::Window* main_window; //v2+++++++++++++++ Gtk::Button* button_sayhello; /* 按钮控件指针 */ Gtk::Label* label_msg; /* 显示问候的标签控件指针 */ /* 由于VariablesMap无法被定义成Glib智能指针,于是我们就 定义一个VariablesMap的标准库的智能指针(下面这句相当于 “Gnome::Glade::VariablesMap* var_map”,只是不用再 “delete var_map;”来释放内存了,它会自动释放)。 通过它,我们能连接控件与变量,进行数据的交换。 */ std::auto_ptr<Gnome::Glade::VariablesMap> var_map; Glib::ustring str_name; /* 获取输入的变量 */ void on_button_sayhello_clicked(); /* 响应按钮点击事件的处理函数 */ //v2--------------- }; #endif // _HELLO_APP_HPP_ |
| 代码: |
| /* tipfoo<tipfoo@gmail#com> 原创作品,版权所有,出处: http://blog.csdn.net/tipfoo/ */ #include "hello-app.hpp" #include <iostream> #include <assert.h> using namespace Gnome::Glade; HelloApp::HelloApp(int argc, char *argv[]): Gtk::Main(argc,argv), main_window(0) { try { ref_xml = Xml::create("helloapp.glade"); } catch(const XmlError& ex) { std::cerr << ex.what() << std::endl; return; } ref_xml->get_widget("main_window", main_window); assert(main_window); //v2+++++++++++++++ /* 取得标签控件的指针,后面用它来设置输出信息 */ ref_xml->get_widget("lable_msg", label_msg); assert(label_msg); /* 将响应"button_sayhello"按钮的点击事件的处理函数连接到 “HelloApp::on_button_sayhello_clicked”函数。 connect_clicked() 函数的第二参数需要一个信号槽;而 sigc::mem_fun()在运行时、动态地返回一个信号槽。其中: 第一个参数是信号槽要操作的类对象实例,在此就是本类的实例; 第二个参数是响应函数的指针,要用全局限定符。 参考(需要安装libsigc++-2.0-doc包): 浏览器打开“ /usr/share/doc/libsigc++-2.0-doc/docs/index.html” 点击“Groups”下的“Functors”。 */ ref_xml->connect_clicked("button_sayhello", sigc::mem_fun(*this, &HelloApp::on_button_sayhello_clicked)); /* 通过ref_xml构建一个VariablesMap实例,并保存其指针于var_map。 然后,我们通过var_map,连接控件与变量,之后就能进行数据的交换了。 */ var_map = std::auto_ptr<VariablesMap> (new VariablesMap(ref_xml)); var_map->connect_widget("entry_name", str_name); //v2--------------- } HelloApp::~HelloApp() { } void HelloApp::show_window() { if (main_window) { run( *main_window ); } } //v2+++++++++++++++ /* 按钮"button_sayhello"的点击事件的处理函数 */ void HelloApp::on_button_sayhello_clicked() { /* 进行数据的交换,将所有控件的数据传入变量中。 这时输入到文本框中的内容就存入“str_name”变量中了。 */ var_map->transfer_widgets_to_variables(); /* 支持Unicode的字符串对象 */ Glib::ustring msg = "Hello, " + str_name + "!"; /* 设置输出信息 */ label_msg->set_text(msg); } //v2--------------- |
| ||||||||||||||||||||||||

286




被折叠的 条评论
为什么被折叠?



