使用 Builder 构造自定义构件

Vala 源代码

/* test.vala
 * Heiher <admin@heiher.info>
 */
 
namespace Hev {
 
public class Window : Gtk.Window {
}
 
static int main(string[] args) {
	Gtk.init(ref args);
 
	try {
		var builder = new Gtk.Builder();
		builder.add_from_file("test.xml");
		builder.connect_signals(null);
 
		Gtk.main();
	} catch(Error e) {
		stderr.printf("Erro: %s\r\n", e.message);
	}
 
	return 0;
}
 
}

test.xml

<?xml version="1.0" encoding="UTF-8" ?>
<interface>
	<object class="HevWindow" id="window_test">
		<property name="visible">TRUE</property>
		<property name="title">Test</property>
		<signal name="delete-event" handler="gtk_main_quit" />
	</object>
</interface>

编译&执行

valac --pkg gtk+-2.0 --pkg gmodule-2.0 test.vala  # gmodule-2.0 必需
./test

Over!

5 thoughts on “使用 Builder 构造自定义构件”

    1. @婉秋 那个是MVC设计,数据是存储在 GtkTextBuffer 对象中的,然后将这个 buffer 嵌入 view 就可以了。具体可以看 gtk-demo 例程。

Leave a Reply

Your email address will not be published. Required fields are marked *