gtk3
Glade with Builder APIの使用
サーチ…
[C ++] gtkmmでGtk :: Builderを使用する
概要
Gtk +は、ユーザインタフェース設計のタスクとプログラミングのタスクが分離されているワークフローをサポートしています。ボタン、メニュー、レイアウトなどのユーザーインターフェイス要素はコードから直接追加できますが、このアプローチはコードを混乱させるだけでなく、プログラマー以外のユーザーのUIを変更することも困難にします。さらに、いくつかのインタフェース要素は、レイアウト構造を保持するためにのみ使用され、ロジックに参加する必要はなく、コードから追加すると、それが長くなります。代わりにGladeを使用してXMLとしてUI記述を生成することができ、Gtk + Builder APIを使用してUIをロードして操作することができます。
ワークフロー
- ドラッグアンドドロップを使用してGladeの UI要素を設計します。 GladeはUI記述を含むXMLファイルを生成します。これは適切なXML構文を記述し、
.glade
拡張子で保存することで手動で行うこともできます。 - ファイルから直接UIをロードする
auto ui = Gtk::Builder::create_from_file("ui.glade");
- 個々のUI要素にアクセスする
// when element doesn't need to be added to another UI element auto ui_elem = Glib::RefPtr<Gtk::Button>::cast_dynamic( ui->get_object("button_UI_id") ); // when element needs to be added to another widget Gtk::Button *btn = nullptr; ui->get_widget<Gtk::Button>("button_UI_id", btn);
例
simple.glade
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkBox" id="cont">
<property name="width_request">200</property>
<property name="height_request">200</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="display_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="wrap">True</property>
<attributes>
<attribute name="weight" value="bold"/>
<attribute name="scale" value="5"/>
<attribute name="foreground" value="#a4a400000000"/>
</attributes>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="display_button">
<property name="label" translatable="yes">Display Message</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</interface>
simple.cpp
#include <gtkmm/application.h>
#include <gtkmm/applicationwindow.h>
#include <gtkmm/button.h>
#include <gtkmm/label.h>
#include <gtkmm/box.h>
#include <gtkmm/builder.h>
class HelloWindow : public Gtk::ApplicationWindow {
Gtk::Box *cont;
Glib::RefPtr<Gtk::Label> display_label;
Glib::RefPtr<Gtk::Button> display_btn;
Glib::RefPtr<Gtk::Builder> ui;
public:
HelloWindow()
: ui{Gtk::Builder::create_from_file("simple.glade")} {
if(ui) {
ui->get_widget<Gtk::Box>("cont", cont);
display_label = Glib::RefPtr<Gtk::Label>::cast_dynamic(
ui->get_object("display_label")
);
display_btn = Glib::RefPtr<Gtk::Button>::cast_dynamic(
ui->get_object("display_button")
);
if(cont && display_label && display_btn) {
display_btn->signal_clicked().connect(
[this]() {
display_label->set_text("Hello World");
});
add(*cont);
}
}
set_title("Simple Gtk::Builder demo");
set_default_size(400, 400);
show_all();
}
};
int main(int argc, char *argv[]) {
auto app = Gtk::Application::create(
argc, argv,
"org.gtkmm.example.HelloApp"
);
HelloWindow hw;
return app->run(hw);
}
出力
Gio :: Resourceを使って
.glade
ファイルからUIを直接読み込むのはすばやく簡単です。しかし、アプリケーションがパッケージ化されると、UIの説明、アイコンなどのイメージをリソースバンドルにまとめることができます。まず、リソース記述をXMLファイルとして作成する必要があります。
resources.xml
<gresources>
<gresource prefix="/unique/prefix/">
<file>icon.png</file>
<!-- text files such as XML can be compressed to save memory -->
<file compressed="true">ui.glade</file>
</gresource>
</gresources>
次に、別の.gresource
ファイル、またはリソースを含む.c
ファイルをアプリケーションの一部としてリンクする文字列データとして作成します。
# generates separate resource file
glib-compile-resources --target=ui.gresource resources.xml
# generates .c file
glib-compile-resources --generate-source resources.xml
次に、アプリケーションコードからリソースバンドルをロードします。
// from separate file
auto resource_bundle = Gio::Resource::create_from_file("ui.gresource");
// from stream of bytes in .c file
auto resource_bundle = Glib:wrap(draw_resource_get_resource());
resource_bundle.register_global();
リソースバンドルのロードUI要素から
auto ui = Gtk::Builder::create_from_resource("/unique/prefix/ui.glade");
Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow