Linux/Unix C C++编程介绍
Linux/Unix C/C++编程 高级篇 Gtkmm-2.4 libGlademm-2.4如果说GTK+相当于m$的vc的sdk的话,那么gtkmm就相当于m$的mfc了,但是其中的用法,原理完全不一样,相像的仅仅是一个针对C,一个包装成了C++.
好,我们现在就开始写一个最简单的gtkmm的hello world,阅读本篇时,须要有前面几篇的基础,搭建开发环境,添加include lib,使用pkg-config等等.
首先,要安装相关开发工具包,Gtkmm-devel-2.4 libGlademm-devel-2.4,安装好后用pkg-config检查路径是否已经添加
如图:1
然后我们用glade写一个gui,只含有一个window跟一个button,相关属性设置如下,工程名:hello
如图:2
如图:3
如图:4
如图:5
建立好的hello.glade
==========hello.glade
<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">
<glade-interface>
<widget class="GtkWindow" id="window_hello">
<property name="visible">True</property>
<property name="title" translatable="yes">window Hello World!</property>
<property name="type">GTK_WINDOW_TOPLEVEL</property>
<property name="window_position">GTK_WIN_POS_NONE</property>
<property name="modal">False</property>
<property name="resizable">True</property>
<property name="destroy_with_parent">False</property>
<property name="decorated">True</property>
<property name="skip_taskbar_hint">False</property>
<property name="skip_pager_hint">False</property>
<property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
<property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
<property name="focus_on_map">True</property>
<property name="urgency_hint">False</property>
<child>
<widget class="GtkButton" id="button_hello">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="label" translatable="yes">Hello World!</property>
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
</widget>
</child>
</widget>
</glade-interface>
====================
然后新建一个C++工程GtkmmHello,把hello.glade考入src目录,并且设置开发环境,目录结构跟环境设置:`pkg-config --cflags gtkmm-2.4` `pkg-config --cflags libglademm-2.4`
如图:6
建立一个源文件main.cc
==========main.cc
#include <gtkmm.h>
#include <libglademm.h>
void on_button_hello_clicked()
{
g_print("Button Hello!/n");
}
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
Glib::RefPtr<Gnome::Glade::Xml> refXml = Gnome::Glade::Xml::create("/root/workspace/GtkmmHello/src/hello.glade");
// get window
Gtk::Window* pWindow = NULL;
pWindow = refXml->get_widget("window_hello", pWindow);
// get button
Gtk::Button* pButton = NULL;
pButton = refXml->get_widget("button_hello", pButton);
// add signal 注意,这里不是sigc::mem_fun,官方教程这里有问题
pButton->signal_clicked().connect(sigc::ptr_fun(&on_button_hello_clicked));
Gtk::Main::run(*pWindow);
return(0);
}
====================
然后保存,运行
如图:7
点击按钮,console会打印文字
如图:8
本文地址:http://www.45fan.com/dnjc/70897.html