组件

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::Box container widget:

m_box.pack_start(m_Button1);
m_box.pack_start(m_Button2);
and here is how to add the Gtk::Box, 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 这一章。

Although gtkmm widget instances have lifetimes and scopes just like those of other C++ classes, gtkmm has an optional time-saving feature that you will see in some of the examples. The Gtk::make_managed() allows you to create a new widget and state that it will become owned by the container into which you place it. This allows you to create the widget, add it to the container and not be concerned about deleting it, since that will occur when the parent container (which may itself be managed) is deleted. You can learn more about gtkmm memory management techniques in the Memory Management chapter.