组件

gtkmm 应用程序由一系列包含了如按钮、文本框之类组件的窗口构成。在一些其它的系统上,组件可能被称为“控件”。对于你的应用程序窗口中的每个组件,在你的代码里就会有一个对应的 C++ 对象。所以当你想控制组件行为的时候,只需要调用这个组件对象的相应方法即可。

Widgets are arranged inside container widgets such as frames and notebooks, in a hierarchy of widgets within widgets. Some of these container widgets, such as Gtk::Grid, are not visible - they exist only to arrange other widgets. Here is some example code that adds 2 Gtk::Button widgets to a Gtk::VBox container widgets:

m_box.pack_start(m_Button1);
m_box.pack_start(m_Button2);
and here is how to add the Gtk::VBox, containing those buttons, to a Gtk::Frame, which has a visible frame and title:
m_frame.add(m_box);

本书中的大部分章节都是讲解特定的组件。要得到更多关于添加组件到容器组件的信息,请看 容器组件 这一章。

尽管你可以使用 C++ 代码来指定窗口和组件的外观和布局,但你可能会发现使用 Glade 来设计你的界面,并且使用 Gtk::Builder 在运行时动态加载界面是更方便。请参考 Glade 与 Gtk::Builder 这一章。

尽管 gtkmm 组件实例拥有像其它 C++ 类那样的生存时间和作用域,gtkmm 还是有一些可选的节省时间的特性,接下来你会在接下来的一些例子中看到。Gtk::manage() 允许你指定一个子组件是被其容器组件所拥有。这将允许你 new 组件、添加到容器中,然后忘记删除它。如果你需要了解更多关于 gtkmm 内存管理技术的话,请看 内存管理 章节。